Tornado:从未检索到任务异常

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

我在Tornado WebSocket服务器中收到异常,但是它在跟踪中没有提供任何信息来知道它是从哪一行代码或程序中的哪一步开始的。我想找出原因,以便尝试捕获异常的来源。

错误跟踪:(未提及文件的任何部分)

[E 200527 21:07:19 base_events:1608] Task exception was never retrieved
    future: <Task finished coro=<WebSocketProtocol13.write_message.<locals>.wrapper() done, defined at /usr/local/lib/python3.7/site-packages/tornado/websocket.py:1102> exception=WebSocketClosedError()>
    Traceback (most recent call last):
    File "/usr/local/lib/python3.7/site-packages/tornado/websocket.py", line 1104, in wrapper
        await fut
    tornado.iostream.StreamClosedError: Stream is closed

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/usr/local/lib/python3.7/site-packages/tornado/websocket.py", line 1106, in wrapper
        raise WebSocketClosedError()
    tornado.websocket.WebSocketClosedError

[E 200527 21:07:19 base_events:1608] Task exception was never retrieved

同一组回溯重复超过16次。

这是我的代码:

import tornado.iostream
import tornado.web
import tornado.gen
import tornado.websocket
import asyncio

class SocketHandler(tornado.websocket.WebSocketHandler):
    waiters = set()
    def initialize(self):
        self.client_name = "newly_connected"

    def get_compression_options(self):
        # Non-None enables compression with default options.
        return {}

    def open(self):
        print('connection opened')
        # SocketHandler.waiters.add(self)

    def on_close(self):
        print("CLOSED!", self.client_name)
        try:
            SocketHandler.waiters.remove(self)
        except KeyError:
            print('tried removing new client')

    def check_origin(self, origin):
        # Override the origin check if needed
        return True

    @classmethod
    async def send_updates(cls, message):
        if len(cls.waiters) < 2:
            while True:
                chat = {}
                # Prevent RuntimeError: Set changed size during iteration
                waiters_copy = cls.waiters.copy()
                for waiter in waiters_copy:
                    try:
                        await waiter.write_message(chat)
                    except tornado.websocket.WebSocketClosedError:
                        pass
                    except tornado.iostream.StreamClosedError:
                        pass
                    except Exception as e:
                        print('Exception e:', waiter.client_name)
                        pass

                # sleep a bit
                await asyncio.sleep(0.05)
        else:
            print('broadcast loop already running')

    async def on_message(self, message):
        print("RECEIVED :", message)
        self.client_name = message
        self.first_serve_cache_on_connnect()
        SocketHandler.waiters.add(self)
        await SocketHandler.send_updates(message)

    def first_serve_cache_on_connnect(self):
        print('serving cache on connect')
        temp_calc_results = self.namespace.results
        try:
            self.write_message(temp_calc_results)
        except Exception as e:
            pass

我已经尝试捕获将消息发送到websocket客户端时可能导致任何错误的异常,但是当客户端连接到服务器时,仍然会发生此错误。

python websocket tornado python-asyncio
1个回答
0
投票

消息“从未检索到任务异常”不是有关缺少的try/except块,而是有关缺少的await。在first_serve_cache_on_connect中,不带write_message的情况下调用await,因此,在引发异常时,first_serve_cache_on_connect不再运行,除日志外无处可去。

这基本上是无害的,但是如果您要清理日志,则需要将first_serve_cache_on_connect设为async def协程,并在await中使用on_message进行调用,然后在write_message中进行调用await

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