龙卷风 websocket 崩溃并出现断言错误

问题描述 投票:0回答:1

我从 websocket 发送数据时遇到一些错误,我认为客户端也在发送数据并与该数据发生冲突

ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 706, in _handle_events
    self._handle_write()
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 964, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 202, in advance
    assert 0 < size <= self._size
AssertionError
ERROR:asyncio:Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 138, in _handle_events
    handler_func(fileobj, events)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 706, in _handle_events
    self._handle_write()
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 964, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 202, in advance
    assert 0 < size <= self._size
AssertionError
'NoneType' object has no attribute 'stream'

我认为解决这个问题的代码是

def send_all(message):
    for ws in web_socket_clients:
        try:
            if not ws.ws_connection.stream.socket:
                web_socket_clients.remove(ws)
            else:
                try:
                    ws.write_message(json.dumps(message))
                except AssertionError as a:
                    pass
        except AssertionError:
            pass
python websocket tornado
1个回答
1
投票

不要直接访问

ws_connection
的属性;不支持此功能。

相反,在处理程序中覆盖 on_close 并从此时的客户端集中删除。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.