python - django_channels“文件末尾的阅读器”异常
问题描述
我已经实现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
解决方案
推荐阅读
- python - 使用python自动登录
- algorithm - 处理图中的负循环
- node.js - 使用“表单数据”的邮递员响应正文为空
- python - 在 Docker 上运行的 Django 中的加密错误
- flutter - 扑。如何检查自动续订订阅是否仍然有效
- dictionary - 使用映射函数-Python更新列表内的字典键
- google-cloud-platform - 您如何授予对两个独立 Google Cloud Platform (GCP) 项目资源的访问权限?
- regex - 如何获取字段包含 ( ) 、 [ ] % 或 + 的行。使用 rlike SparkSQL 函数
- asp.net-mvc - 在每个 Visual Studio 应用发布上重置 IIS 应用程序池标识权限
- c# - 如何根据行中另一列的值动态地将值分配给 MVC 网格