使用run_in_executor时未捕获TimeoutError问题求助
问题:使用Tornado+run_in_executor运行阻塞库时频繁出现Future异常未捕获的TimeoutError
我开发了一个程序,负责处理设备传来的数据流并写入WebSocket供Web应用消费。读取设备并通过yield输出计算值的库是同步阻塞的,不想重写为异步,所以用asyncio的run_in_executor放到线程里跑(框架用的是Tornado来支持Web请求)。
代码能正常运行,但频繁弹出「Future exception was never retrieved」错误,根源是执行器里运行的阻塞函数触发了TimeoutError。
注意:如果不把asyncio事件策略设为tornado.platform.asyncio.AnyThreadEventLoopPolicy(),执行器里的函数根本跑不起来,会一直报「there is no current event loop in ThreadExecutor」,目前没找到其他解决办法。
class ClientHandler(tornado.websocket.WebSocketHandler): clients = set() def __init__(self, *args, **kwargs): self.config = kwargs.pop("config") super().__init__(*args, **kwargs) self.started = False def on_message(self, message): if message == "data": WebClientStreamHandler.clients.add(self) self.start_client() def write_data(self, message: str): for client in WebClientStreamHandler.clients: client.write_message(message) def start_client(self): if self.started: return asyncio.set_event_loop_policy(tornado.platform.asyncio.AnyThreadEventLoopPolicy()) sync_func = partial(run_client_forever, self.config) loop = tornado.ioloop.IOLoop.current() loop.run_in_executor(None, sync_func) self.started = True
错误信息(精简版)
ERROR:asyncio:Future exception was never retrieved future: <Future finished exception=TimeoutError('timed out') created at /usr/lib/python3.11/asyncio/base_events.py:427> Traceback: ... File "/path/src/cps_demo/web_stream_handler.py", line 118, in start_client loop.run_in_executor(None, sync_func) File "/path/venv/lib/python3.11/site-packages/tornado/platform/asyncio.py", line 266, in run_in_executor return self.asyncio_loop.run_in_executor(executor, func, *args) File "/usr/lib/python3.11/asyncio/base_events.py", line 828, in run_in_executor return futures.wrap_future( File "/usr/lib/python3.11/asyncio/futures.py", line 417, in wrap_future new_future = loop.create_future() File "/usr/lib/python3.11/asyncio/base_events.py", line 427, in create_future return futures.Future(loop=self) TimeoutError: timed out
内容的提问来源于stack exchange,提问作者bluppfisk
相关产品推荐
相关产品推荐

