RuntimeError: 无法对关闭的处理程序执行操作。 Django 和颤振
Posted
技术标签:
【中文标题】RuntimeError: 无法对关闭的处理程序执行操作。 Django 和颤振【英文标题】:RuntimeError: unable to perform operation on the handler is closed. Django and Flutter 【发布时间】:2021-09-25 11:19:07 【问题描述】:我是 WebSocket 编程和 Dart/Flutter 编程的新手,但对 Django/Python 并不陌生。这个玩具项目是我空闲时间自学的
我从 here 中挑选了很棒的 Django 样板,并按照频道的教程 here 进行操作
Web 浏览器 Chrome 能够重现该示例。接下来我试试 Flutter tutorial
我希望手机用户能够收听消息并发送消息。 但是我的手机只能接收来自网络用户的消息。
问题: 当手机发送消息时。 Django 断开其连接并在终端中引发此错误
django | ERROR: closing handshake failed
django | Traceback (most recent call last):
django | File "/usr/local/lib/python3.9/site-packages/websockets/legacy/server.py", line 232, in handler
django | await self.close()
django | File "/usr/local/lib/python3.9/site-packages/websockets/legacy/protocol.py", line 779, in close
django | await asyncio.shield(self.close_connection_task)
django | File "/usr/local/lib/python3.9/site-packages/websockets/legacy/protocol.py", line 1309, in close_connection
django | self.transport.write_eof()
django | File "uvloop/handles/stream.pyx", line 696, in uvloop.loop.UVStream.write_eof
django | File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
django | RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x55ed26012b80>; the handler is closed
django | ERROR 2021-09-25 11:05:01,093 server 11 140336674826048 closing handshake failed
django | Traceback (most recent call last):
django | File "/usr/local/lib/python3.9/site-packages/websockets/legacy/server.py", line 232, in handler
django | await self.close()
django | File "/usr/local/lib/python3.9/site-packages/websockets/legacy/protocol.py", line 779, in close
django | await asyncio.shield(self.close_connection_task)
django | File "/usr/local/lib/python3.9/site-packages/websockets/legacy/protocol.py", line 1309, in close_connection
django | self.transport.write_eof()
django | File "uvloop/handles/stream.pyx", line 696, in uvloop.loop.UVStream.write_eof
django | File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
django | RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x55ed26012b80>; the handler is closed
django | INFO: connection closed
问题: 如何解决这个错误?
重现错误:
-
从repository结帐
master
docker-compose.yml -f loca.lyml up
从study_websocket
运行 Flutter
网络浏览器转到http://localhost:8000/chat/cat/
。我把硬编码cat
放在频道`url中
webbrowser 发送消息
手机发送消息。此时
【问题讨论】:
【参考方案1】:在发送到 WebSocket 之前,我必须使用 json.encode()
我的有效负载
_channel.sink.add(json.encode('message': _controller.text))
【讨论】:
以上是关于RuntimeError: 无法对关闭的处理程序执行操作。 Django 和颤振的主要内容,如果未能解决你的问题,请参考以下文章
(Python) Discord 机器人代码返回“RuntimeError:无法关闭正在运行的事件循环”
RuntimeError:事件循环已关闭任务被破坏但它正在等待 Discord Python
尝试python多处理的Windows上的RuntimeError
Discord.py - 重新启动命令错误 - RuntimeError:事件循环已关闭