gpt4 book ai didi

Python3.5 Asyncio - 防止任务异常转储到标准输出?

转载 作者:行者123 更新时间:2023-12-03 19:57:26 24 4
gpt4 key购买 nike

我的程序有一个基于文本的界面(asciimatics 模块),它使用 asyncio 和 discord.py 模块,偶尔当我的 wifi 适配器出现故障时,我会遇到如下异常:

Task exception was never retrieved
future: <Task finished coro=<WebSocketCommonProtocol.run() done, defined at /home/mike/.local/lib/python3.5/site-packages/websockets/protocol.py:428> exception=ConnectionResetError(104, 'Connection reset by peer')>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/home/mike/.local/lib/python3.5/site-packages/websockets/protocol.py", line 434, in run
msg = yield from self.read_message()
File "/home/mike/.local/lib/python3.5/site-packages/websockets/protocol.py", line 456, in read_message
frame = yield from self.read_data_frame(max_size=self.max_size)
File "/home/mike/.local/lib/python3.5/site-packages/websockets/protocol.py", line 511, in read_data_frame
frame = yield from self.read_frame(max_size)
File "/home/mike/.local/lib/python3.5/site-packages/websockets/protocol.py", line 546, in read_frame
self.reader.readexactly, is_masked, max_size=max_size)
File "/home/mike/.local/lib/python3.5/site-packages/websockets/framing.py", line 86, in read_frame
data = yield from reader(2)
File "/usr/lib/python3.5/asyncio/streams.py", line 670, in readexactly
block = yield from self.read(n)
File "/usr/lib/python3.5/asyncio/streams.py", line 627, in read
yield from self._wait_for_data('read')
File "/usr/lib/python3.5/asyncio/streams.py", line 457, in _wait_for_data
yield from self._waiter
File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/asyncio/selector_events.py", line 662, in _read_ready
data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer

这个异常不是致命的,尽管它可以重新连接程序 - 我想要做的是防止这个异常转储到标准输出并弄乱我的文本界面。

我尝试使用 ensure_future 来处理它,但它似乎不起作用。我错过了什么吗:
@asyncio.coroutine
def handle_exception():
try:
yield from WebSocketCommonProtocol.run()
except Exception:
print("SocketException-Retrying")


asyncio.ensure_future(handle_exception())
#start discord client
client.run(token)

最佳答案

Task exception was never retrieved - 实际上不是传播到标准输出的异常,而是一条日志消息,警告您从未在您的一项任务中检索到异常。您可以找到详细信息here .

我想,在您的情况下避免此消息的最简单方法是手动从任务中检索异常:

coro = WebSocketCommonProtocol.run()  # you don't need any wrapper
task = asyncio.ensure_future(coro)

try:

#start discord client
client.run(token)

finally:

# retrieve exception if any:
if task.done() and not task.cancelled():
task.exception() # this doesn't raise anything, just mark exception retrieved

关于Python3.5 Asyncio - 防止任务异常转储到标准输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50192934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com