首页 > 解决方案 > 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

标签: pythondjangowebsocketdjango-channels

解决方案


推荐阅读