Re: Django 2.0.2, Channels 2.0.2 and Celery 4.1 Issue
Plese, Can you send us a basic example of celery 4 and channels 2? El viernes, 2 de marzo de 2018, 19:36:08 (UTC+1), G Broten escribió: > > Hi All: > I'm migrating a small application from Django 1.x/Channels 1.x to Django > 2.0.2 and Channels 2.0. I've run into an issue whose cause I'm trying to > determine. It could be due to a failure on my part to correctly implement > the channel_layer or it could be due to an > incompatibility with Celery 4.1. The basics are: > - Run a periodic Celery task > - Use the channel_layer to perform a group_send > - Have the consumer receive the group_send event and push a json message > over the socket > > Show below is my simple consumer.py module: > class mstatusMessage(AsyncJsonWebsocketConsumer): > > # WebSocket event handlers > > async def connect(self): > """ > Called when the websocket is handshaking as part of initial > connection. > """ > logging.info("### Connected ###") > # Accept the connection > await self.accept() > > # Add to the group so they get messages > await self.channel_layer.group_add( > settings.CHANNEL_GROUP, > self.channel_name, > ) > > async def disconnect(self, code): > """ > Called when the WebSocket closes for any reason. > """ > # Remove them from the group > await self.channel_layer.group_discard( > settings.CHANNEL_GROUP, > self.channel_name, > ) > > # Handlers for messages sent over the channel layer > > # These helper methods are named by the types we send - so epics.join > becomes epics_join > async def epics_message(self, event): > """ > Called when the Celery task queries Epics. > """ > logging.error("### Received Msg ###") > # Send a message down to the client > await self.send_json( > { > "text": event["message"], > }, > ) > > The routing is simple: > application = ProtocolTypeRouter({ > "websocket": mstatusMessage > }) > > The Celery task is as follows: > @shared_task > def updateData(param): > > logger.error('# updateData #') > > # # Get an instance of the channel layer for > # # inter task communications > channel_layer = get_channel_layer() > > channel_layer.group_send( > settings.CHANNEL_GROUP, > {"type": "epics.message", "text": "Hello World"}, > ) > > The results are promising as the websocket connect opens successfully and > the Celery task run as show by the debugging output given below: > 127.0.0.1:59818 - - [02/Mar/2018:09:32:11] "GET /" 200 100639 > 127.0.0.1:59844 - - [02/Mar/2018:09:32:12] "WSCONNECTING /epics/" - - > 2018-03-02 09:32:12,280 INFO ### Connected ### > 127.0.0.1:59844 - - [02/Mar/2018:09:32:12] "WSCONNECT /epics/" - - > [2018-03-02 09:32:12,312: ERROR/ForkPoolWorker-2] > mstatus.tasks.updateData[8d329e61-]: # updateData # > [2018-03-02 09:32:13,310: ERROR/ForkPoolWorker-2] > mstatus.tasks.updateData[786f51a6-]: # updateData # > > BUT ... although the Celery task runs the consumer never > receives a message via the channel layer. This could be due to an > implementation error or, maybe, a compatibility issue. The application > doesn't crash but the following warning is issued: > > [2018-03-02 09:32:02,105: WARNING/ForkPoolWorker-2] > /mstatus/mstatus/tasks.py:33: RuntimeWarning: coroutine > 'RedisChannelLayer.group_send' was never awaited > {"type": "epics.message", "text": "Hello World"}, > > This warning appears related to the Python asyncio functionality. Under > the Celery task module, the channel_layer.group_send > doesn't use the await directive as it's inclusion hangs the Celery task. > Changing the Celery task to: > async def updateData(param): > > logger.error('# updateData #') > > # # Get an instance of the channel layer for > # # inter task communications > channel_layer = get_channel_layer() > > await channel_layer.group_send( > settings.CHANNEL_GROUP, > {"type": "epics.message", "text": "Hello World"}, > ) > > This results in the following runtime warning and the Celery task fails to > run (the debug message is never printed) : > > [2018-03-02 09:45:19,804: WARNING/ForkPoolWorker-2] > /home/broteng/.pyenv/versions/3.6.3/envs/djchannels2/lib/python3.6/site-packages/billiard/pool.py:358: > > RuntimeWarning: coroutine 'updateData' was never awaited > result = (True, prepare_result(fun(*args, **kwargs))) > > I'm sure these warnings are understood by someone who can provide guidance > with respect to a solution. > > Thanks, > > G Broten > > Reference: > > The application has be tried under two OS versions: > CentOS 7.4 > Alpine Linux 3.7 > > A partial pip list of the significant packages: > asgi-redis (1.4.3) > asgiref (2.1.6) > async-timeout (2.0.0) >
Help with Channels 2.x and Celery 4.x
Hi!! I need a basic example of a program with channels 2.x (Django 2.x) that when it fulfills a condition, it calls a task celery 4.x and updates its status in a message in the html. (for example https://vincenttide.com/blog/1/django-channels-and-celery-example/) But i have the following error in consumer.py in "def mensaje": *raise ValueError("No handler for message type %s" % message["type"]):* Mi code in *consumers.py*: from channels.generic.websocket import AsyncJsonWebsocketConsumerfrom .tasks import PruebaCeleryclass Consumer(AsyncJsonWebsocketConsumer): async def connect(self): print(self.channel_name) await self.accept() print("[BACK]:Cliente conectado") async def disconnect(self, close_code): return None async def receive_json(self, contenido): try: if True: print("[BACK]:Datos recividos",contenido) await self.mensaje(contenido) except: pass async def mensaje(self, contenido): print("[BACK]:Si se cumple la condicion se envia mensaje") print(contenido["Clave_Tipo_Filtro"]) TipoFiltro = contenido["Clave_Tipo_Filtro"] if TipoFiltro == "Filtro_Paso_Bajo": print("dentro") mensaje = PruebaCelery.delay(self.channel_name) print("Here") print ("hola %s" %str(mensaje["text"])) print("Out") print ("Task ID: %s" %mensaje) await self.send("Task ID: %s" %str(mensaje)) await self.send("se ha ejecutado celery") else: print("no entra") return None Mi code in *tasks.py*: #De celery from Filtros.celery import app import json from channels.layers import get_channel_layer from asgiref.sync import AsyncToSync @app.task def PruebaCelery(channel_name): # responder al cliente channel_layer = get_channel_layer() AsyncToSync(channel_layer.send)( channel_name, {"type": "Test", "text": json.dumps("Hi im celery"), }) Mi code in *html*: *how could I print "hi im celery" in the html?* *
I have a problem whit new celery and channels
Hi! I'm trying to create a chat that when it meets the condition of "hola" is sent by a task in celery. However, when it enters the condition the status is not updated, can someone help me? I leave my code, please help me! Please help me, thx! And sorry for my english :s *consumers.py* import jsonfrom channels.generic.websocket import AsyncWebsocketConsumerfrom .tasks import MensajeAlGrupo class Consumidor(AsyncWebsocketConsumer): async def connect(self): # Join room group await self.channel_layer.group_add( 'grupo', self.channel_name ) await self.accept() async def disconnect(self, close_code): await self.channel_layer.group_discard( 'grupo', self.channel_name ) # Receive message from WebSocket async def receive(self, text_data): text_data_json = json.loads(text_data) message = text_data_json['message'] if str(message) == "hola": MensajeAlGrupo.delay() else: # Send message to room group await self.channel_layer.group_send( 'grupo', { 'type': 'chat_message', 'message': message } ) # Receive message from room group async def chat_message(self, event): message = event['message'] # Send message to WebSocket await self.send(text_data=json.dumps({ 'message': message })) *tasks.py* #De celeryfrom Filtros.celery import appfrom channels.layers import get_channel_layer @app.task() async def MensajeAlGrupo(): channel_layer = get_channel_layer() await channel_layer.group_send( 'grupo', {"type": "chat_message", "message": "Hello World"}, ) *celery.py* #De celeryfrom __future__ import absolute_import, unicode_literalsimport osfrom celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Filtros.settings') app = Celery('Proyecto') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() app.conf.update( BROKER_URL = 'redis://127.0.0.1:6379/0',) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/873fa6e7-644d-485f-aabc-c4d384c758cf%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: I have a problem whit new celery and channels
> > Hi! > > I'm trying to create a chat that when it meets the condition of "hola" is > sent by a task in celery. However, when it enters the condition the status > is not updated, can someone help me? > > > I leave my code, please help me! > > > Please help me, thx! And sorry for my english :s > And the error is [2018-04-18 01:11:25,798: WARNING/MainProcess] c:\python\lib\site-packages\celery\concurrency\base.py:31: RuntimeWarning: coroutine 'MensajeAlGrupo' was never awaited ret = target(*args, **kwargs) > > *consumers.py* > > import jsonfrom channels.generic.websocket import > AsyncWebsocketConsumerfrom .tasks import MensajeAlGrupo > class Consumidor(AsyncWebsocketConsumer): > async def connect(self): > # Join room group > await self.channel_layer.group_add( > 'grupo', > self.channel_name > ) > await self.accept() > > async def disconnect(self, close_code): > await self.channel_layer.group_discard( > 'grupo', > self.channel_name > ) > > # Receive message from WebSocket > async def receive(self, text_data): > text_data_json = json.loads(text_data) > message = text_data_json['message'] > if str(message) == "hola": > MensajeAlGrupo.delay() > else: > > # Send message to room group > await self.channel_layer.group_send( > 'grupo', > { > 'type': 'chat_message', > 'message': message > } > ) > > # Receive message from room group > async def chat_message(self, event): > message = event['message'] > > # Send message to WebSocket > await self.send(text_data=json.dumps({ > 'message': message > })) > > > *tasks.py* > > #De celeryfrom Filtros.celery import appfrom channels.layers import > get_channel_layer > > @app.task() > async def MensajeAlGrupo(): > channel_layer = get_channel_layer() > await channel_layer.group_send( > 'grupo', > {"type": "chat_message", "message": "Hello World"}, > ) > > > > *celery.py* > > #De celeryfrom __future__ import absolute_import, unicode_literalsimport > osfrom celery import Celery > # set the default Django settings module for the 'celery' program. > os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Filtros.settings') > > app = Celery('Proyecto') > app.config_from_object('django.conf:settings', namespace='CELERY') > app.autodiscover_tasks() > > app.conf.update( > BROKER_URL = 'redis://127.0.0.1:6379/0',) > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a4da7470-a707-49c0-878d-27b4a9b3c0a0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Problem with Channels and Celery... Please help me!
I'm trying to create a chat that when it meets the condition of "hola" is sent by a task in celery. However, when it enters the condition the status is not updated, can someone help me? I leave my code, please help! Mi error is: > [2018-04-18 01:11:25,798: WARNING/MainProcess] c:\python\lib\site-packages\celery\concurrency\base.py:31: RuntimeWarning: coroutine 'MensajeAlGrupo' was never awaited ret = target(*args, **kwargs) ***consumers.py*** import json from channels.generic.websocket import AsyncWebsocketConsumer from .tasks import MensajeAlGrupo class Consumidor(AsyncWebsocketConsumer): async def connect(self): # Join room group await self.channel_layer.group_add( 'grupo', self.channel_name ) await self.accept() async def disconnect(self, close_code): await self.channel_layer.group_discard( 'grupo', self.channel_name ) # Receive message from WebSocket async def receive(self, text_data): text_data_json = json.loads(text_data) message = text_data_json['message'] if str(message) == "hola": MensajeAlGrupo.delay() else: # Send message to room group await self.channel_layer.group_send( 'grupo', { 'type': 'chat_message', 'message': message } ) # Receive message from room group async def chat_message(self, event): message = event['message'] # Send message to WebSocket await self.send(text_data=json.dumps({ 'message': message })) ***Tasks.py*** #De celery from Filtros.celery import app from channels.layers import get_channel_layer @app.task() async def MensajeAlGrupo(): channel_layer = get_channel_layer() await channel_layer.group_send( 'grupo', {"type": "chat_message", "message": "Hello World"}, ) ***celery.py*** #De celery from __future__ import absolute_import, unicode_literals import os from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Filtros.settings') app = Celery('Proyecto') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() app.conf.update( BROKER_URL = 'redis://127.0.0.1:6379/0', ) Please help me, thx! Sorry for my english :s -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b6eb8abf-86c2-4023-be4b-a2bd81d459e3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Error with channels and celery
I'm trying to create a chat that when it meets the condition of "hola" is sent by a task in celery. However, when it enters the condition the status is not updated, can someone help me? I leave my code, please help! Mi error is: raise OSError(err, 'Connect call failed %s' % (address,)) ConnectionRefusedError: [Errno 10061] Connect call failed ('::1', 6379) *consumers.py* import jsonfrom channels.generic.websocket import AsyncWebsocketConsumerfrom .tasks import MensajeAlGrupo class Consumidor(AsyncWebsocketConsumer): async def connect(self): # Join room group await self.channel_layer.group_add( 'grupo', self.channel_name ) await self.accept() async def disconnect(self, close_code): await self.channel_layer.group_discard( 'grupo', self.channel_name ) # Receive message from WebSocket async def receive(self, text_data): text_data_json = json.loads(text_data) message = text_data_json['message'] if str(message) == "hola": MensajeAlGrupo.delay() else: # Send message to room group await self.channel_layer.group_send( 'grupo', { 'type': 'chat_message', 'message': message } ) # Receive message from room group async def chat_message(self, event): message = event['message'] # Send message to WebSocket await self.send(text_data=json.dumps({ 'message': message })) *Tasks.py* #De celeryfrom Filtros.celery import appfrom channels.layers import get_channel_layer @app.task()def MensajeAlGrupo(): channel_layer = get_channel_layer() async_to_sync(channel_layer.group_send)( 'grupo', {"type": "chat.message", "message": "Hello World"}, ) *celery.py* #De celeryfrom __future__ import absolute_import, unicode_literalsimport osfrom celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Filtros.settings') app = Celery('Proyecto') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() app.conf.update( BROKER_URL = 'redis://127.0.0.1:6379/0',) Please help me, thx! Sorry for my english :s -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e5cebcf-8bbd-442a-81cd-092c6f261fed%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.