关门时间过长被杀

Sus*_*S N 11 django django-channels

WARNING Application instance <Task pending coro=<__call__() running at 
/home/developer/projects/tabcon/tabcon_env/lib/python3.5/site-packages/channels/http.py:191>
wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/lib/python3.5/asyncio/futures.py:431, Task._wakeup()]>> 
for connection <WebRequest at 0x7ffa40e17b00 method=GET uri=/ clientproto=HTTP/1.1> took too long to shut down and was killed. 
Run Code Online (Sandbox Code Playgroud)

出现上述异常,整个服务停止运行。

我在用:

  • 频道==2.0
  • channel_redis==2.2.1
  • 达芙妮==2.2.2
  • 姜戈==2.1
  • channel_redis==2.2.1

K.A*_*K.A 1

当我尝试从客户端断开客户端连接时(通过使用 Postman 应用程序),我遇到了同样的问题,并且我找到了以下解决方案,通过在 websoket 断开连接方法中引发 StopConsumer() 异常,请查看以下官方 DOC 链接https://channels.readthedocs.io/en/stable/topics/consumers.html#ending-consumers

这是一个简单的例子:

from channels.consumer import SyncConsumer
from channels.exceptions import StopConsumer


class MySyncConsumer(SyncConsumer):

    def websocket_connect(self, event):
        print('websocket connected1...', event)

        self.send({
            'type': 'websocket.accept'
        })
        print('websocket connected2...', event)

    def websocket_receive(self, event):
        print('websocket recived message...', event)

    def websocket_disconnect(self, event):
        print('websocket disconnected...', event)  
        raise StopConsumer()
Run Code Online (Sandbox Code Playgroud)

我不知道我的情况是否与您的情况相同,但我希望这有帮助。