Python django“U频道”;“文件末尾的读取器”;例外
我已经实现了Python django“U频道”;“文件末尾的读取器”;例外,python,django,websocket,django-channels,Python,Django,Websocket,Django Channels,我已经实现了django\u频道AsyncJsonWebsocketConsumer消费者,他们订阅了“chat”组: from channels.generic.websocket import AsyncJsonWebsocketConsumer class CeleryTaskConsumer(AsyncJsonWebsocketConsumer): async def connect(self): await self.accept() awai
django\u频道AsyncJsonWebsocketConsumer
消费者,他们订阅了“chat”
组:
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class CeleryTaskConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add(group='chat', channel=self.channel_name)
async def new_message_handler(self, event):
await self.send_json(content={'event': event})
import channels.layers
from asgiref.sync import async_to_sync
def send_notification():
channel_layer = channels.layers.get_channel_layer()
async_to_sync(channel_layer.group_send)("chat", {
'type': 'new.message.handler',
'message': 'Hello world!'
})
然后我定期向聊天组发送消息:
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class CeleryTaskConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add(group='chat', channel=self.channel_name)
async def new_message_handler(self, event):
await self.send_json(content={'event': event})
import channels.layers
from asgiref.sync import async_to_sync
def send_notification():
channel_layer = channels.layers.get_channel_layer()
async_to_sync(channel_layer.group_send)("chat", {
'type': 'new.message.handler',
'message': 'Hello world!'
})
一切正常,但我经常遇到这样的例外:
Exception inside application: Reader at end of file
File "/usr/lib64/python3.6/asyncio/coroutines.py", line 129, in throw
return self.gen.throw(type, value, traceback)
File "/venv/lib64/python3.6/site-packages/channels/consumer.py", line 59, in __call__
[receive, self.channel_receive], self.dispatch
File "/usr/lib64/python3.6/asyncio/coroutines.py", line 129, in throw
return self.gen.throw(type, value, traceback)
File "/venv/lib64/python3.6/site-packages/channels/utils.py", line 59, in await_many_dispatch
await task
File "/venv/lib64/python3.6/site-packages/channels/utils.py", line 51, in await_many_dispatch
result = task.result()
File "/usr/lib64/python3.6/asyncio/coroutines.py", line 126, in send
return self.gen.send(value)
File "/venv/lib64/python3.6/site-packages/channels_redis/core.py", line 429, in receive
real_channel
File "/usr/lib64/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/venv/lib64/python3.6/site-packages/channels_redis/core.py", line 484, in receive_single
index, channel_key, timeout=self.brpop_timeout
File "/usr/lib64/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/venv/lib64/python3.6/site-packages/channels_redis/core.py", line 327, in _brpop_with_clean
await connection.eval(cleanup_script, keys=[], args=[channel, backup_queue])
File "/venv/lib64/python3.6/site-packages/aioredis/commands/scripting.py", line 12, in eval
return self.execute(b'EVAL', script, len(keys), *(keys + args))
File "/venv/lib64/python3.6/site-packages/aioredis/commands/__init__.py", line 51, in execute
return self._pool_or_conn.execute(command, *args, **kwargs)
File "/venv/lib64/python3.6/site-packages/aioredis/connection.py", line 319, in execute
raise ConnectionClosedError(msg)
Reader at end of file
我不知道是什么原因造成的,如何修复
我正在使用Python3.6.6
、redis server5.0.4
和这些Python包:
Django==2.1.5
aioredis==1.2.0
asgiref==2.3.2
async-timeout==3.0.1
asyncio==3.4.3
channels==2.1.7
channels_redis==2.3.3
daphne==2.2.5
hiredis==0.2.0
msgpack==0.6.0
twisted==18.9.0
你试过'new.message.handler'
-->'new\u message\u handler'
?@BearBrown是的,我试过了。在这两种情况下都会调用新消息处理程序。正如我提到的,我的示例按预期工作,但有时我会出现随机错误。这是怎么回事?在accept()
@PankajSharma之前写这行wait self.channel\u layer.group\u add(group='chat',channel=self.channel\u name)
,我刚刚尝试了这个修改,但是没有效果。我仍然随机得到“文件末尾的读取器”
异常。