频道和芹菜错误
Error with channels and celery
我正在尝试创建一个聊天,当它满足 "hola" 的条件时,由 celery 中的任务发送。但是,当它进入状态时状态没有更新,有人可以帮助我吗?
我留下我的代码,请帮忙!
Mi错误是:
raise OSError(err, 'Connect call failed %s' % (address,))
ConnectionRefusedError: [Errno 10061] Connect call failed ('::1', 6379)
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()
def MensajeAlGrupo():
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
'grupo',
{"type": "chat.message", "message": "Hello World"},
)
请帮助我,谢谢!
对不起我的英语:s
问题出在 settings.py in "localhost" in windows,将 "localhost" 更改为 127.0.0.1
Settings.py
#------------------ CONFIGURACION DE LOS CHANNELS -----------------#
redis_host = os.environ.get('REDIS_HOST', '127.0.0.1')
# Channel layer definitions
CHANNEL_LAYERS = {
"default": {
# This example app uses the Redis channel layer implementation channels_redis
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(redis_host, 6379)],
},
},
}
# ASGI_APPLICATION should be set to your outermost router
ASGI_APPLICATION = 'Filtros.routing.application'
#celery settings
CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
Celery.py
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('proj')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
我正在尝试创建一个聊天,当它满足 "hola" 的条件时,由 celery 中的任务发送。但是,当它进入状态时状态没有更新,有人可以帮助我吗?
我留下我的代码,请帮忙!
Mi错误是:
raise OSError(err, 'Connect call failed %s' % (address,)) ConnectionRefusedError: [Errno 10061] Connect call failed ('::1', 6379)
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()
def MensajeAlGrupo():
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
'grupo',
{"type": "chat.message", "message": "Hello World"},
)
请帮助我,谢谢!
对不起我的英语:s
问题出在 settings.py in "localhost" in windows,将 "localhost" 更改为 127.0.0.1
Settings.py
#------------------ CONFIGURACION DE LOS CHANNELS -----------------#
redis_host = os.environ.get('REDIS_HOST', '127.0.0.1')
# Channel layer definitions
CHANNEL_LAYERS = {
"default": {
# This example app uses the Redis channel layer implementation channels_redis
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(redis_host, 6379)],
},
},
}
# ASGI_APPLICATION should be set to your outermost router
ASGI_APPLICATION = 'Filtros.routing.application'
#celery settings
CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
Celery.py
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('proj')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))