我已经实现了django_channels
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})
然后我定期向"chat"
组发送一条消息:
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
我不确定是什么原因引起的以及如何解决?
我正在使用Python 3.6.6
,redis服务器5.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