Django 频道错误“关闭时间太长,已被杀死". [英] Django channel Error "took too long to shut down and was killed."
问题描述
我在控制台日志中收到此错误,并且在表单提交时它不断加载并没有将数据发布到服务器.
<块引用>/home/Python/Working/Benutzerverwaltung/env/lib/python3.6/site-packages/channels/sessions.py:183>wait_for=<Future pending cb=[<TaskWakeupMethWrapper 对象在0x7fab9fe51408>()]>>用于连接 花了太长时间关闭并被杀死.
这是我关闭频道的代码.
async def disconnect(self, code):async_to_sync(self.channel_layer.group_discard)(self.room_group_name,自我渠道)等待 self.close()async def websocket_disconnect(self, event):打印(断开连接",事件)等待 self.send({类型":websocket.close"})
如何解决这个问题?
这个错误主要是因为协程比它应该的时间更长.
这个具体案例
在这种情况下,AsyncWebsocketConsumer.websocket_disconnect()
被覆盖但没有调用 super()
,这意味着 StopConsumer()
't 运行(见 channels/generic/websocket.py:228).也许根本不要覆盖 websocket_disconnect
,因为在这个例子中没有任何东西可以证明它的合理性.
另请注意,async_to_sync
适用于同步使用者,但这是异步使用者.而是使用:
await self.channel_layer.group_discard(self.room_group_name,自我渠道)
await self.close()
不是必需的,因为断开连接已经发生.删除该行.
AsyncHttpConsumer
类似在 AsyncHttpConsumer
中,我经常犯的错误是调用 await self.send_response(...)
但忘记调用 return
之后,因此该功能将在您没想到的时候继续运行.
AsyncHttpConsumer
也有一个关于开放错误报告不在 handle()
中出现异常.目前唯一的选择是添加额外的打印/日志记录行来确定哪些正在/未运行.
当心 django-debug-toolbar
另外值得注意的是,将 debug-toolbar
添加到您的 INSTALLED_APPS
将使异步消费者中的异常静音.请参阅此处的讨论.当心!
I am getting this error on my console log, and on form submit it keeps loading does not post data to the server.
/home/Python/Working/Benutzerverwaltung/env/lib/python3.6/site-packages/channels/sessions.py:183> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fab9fe51408>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 59462] path=b'/ws/stream/Sales'> took too long to shut down and was killed.
Here is my code for closing the channel.
async def disconnect(self, code):
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel
)
await self.close()
async def websocket_disconnect(self, event):
print("Disconnect", event)
await self.send({
"type": "websocket.close"
})
How to fix this?
This error is broadly because a coroutine is hanging around longer than it should.
This specific case
In this case, AsyncWebsocketConsumer.websocket_disconnect()
is being overridden but is not calling super()
which means that StopConsumer()
isn't run (see channels/generic/websocket.py:228). Perhaps don't override websocket_disconnect
at all, since there's nothing in this example that justifies it.
Also note that async_to_sync
is intended for sync consumers, but this is an async consumer. Instead use:
await self.channel_layer.group_discard(
self.room_group_name,
self.channel
)
The await self.close()
isn't required as disconnect has already occurred. Remove that line.
AsyncHttpConsumer
Similarly in an AsyncHttpConsumer
the mistake I've often made is to call await self.send_response(...)
but then forget to call return
afterwards, so the function will continue on when you didn't expect it to.
AsyncHttpConsumer
also has an open bug report about not surfacing exceptions within handle()
. The only option currently is to adding extra print/logging lines to figure out what is/isn't running.
Beware of django-debug-toolbar
Also worth noting is that adding debug-toolbar
to your INSTALLED_APPS
will silence exceptions in your async consumers. See discussion here. Beware!
这篇关于Django 频道错误“关闭时间太长,已被杀死".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!