我在项目中使用asyncio,并遇到了这种奇怪的行为。
import asyncio
def schedule_something():
global f
tsk = asyncio.async(do_something())
f = tsk #If this line is commented out, exceptions can be heard.
@asyncio.coroutine
def do_something():
raise Exception()
loop = asyncio.get_event_loop()
loop.call_soon(schedule_something)
loop.run_forever()
loop.close()
由于某些原因,在调用asyncio.async()
时存储结果任务会阻止异常执行任何操作。
有人可以阐明这种情况吗?我需要有一种方法可以在当前项目中捕获异常。
这是因为仅在Task
被销毁而没有检索到其结果的情况下才会引发异常。当将Task
分配给全局变量时,它将始终具有有效的引用,因此不会被破坏。在asyncio / futures.py中有一个文档字符串,对此进行了详细说明:
class _TracebackLogger:
"""Helper to log a traceback upon destruction if not cleared.
This solves a nasty problem with Futures and Tasks that have an
exception set: if nobody asks for the exception, the exception is
never logged. This violates the Zen of Python: 'Errors should
never pass silently. Unless explicitly silenced.'
However, we don't want to log the exception as soon as
set_exception() is called: if the calling code is written
properly, it will get the exception and handle it properly. But
we *do* want to log it if result() or exception() was never called
-- otherwise developers waste a lot of time wondering why their
buggy code fails silently.
An earlier attempt added a __del__() method to the Future class
itself, but this backfired because the presence of __del__()
prevents garbage collection from breaking cycles. A way out of
this catch-22 is to avoid having a __del__() method on the Future
class itself, but instead to have a reference to a helper object
with a __del__() method that logs the traceback, where we ensure
that the helper object doesn't participate in cycles, and only the
Future has a reference to it.
The helper object is added when set_exception() is called. When
the Future is collected, and the helper is present, the helper
object is also collected, and its __del__() method will log the
traceback. When the Future's result() or exception() method is
called (and a helper object is present), it removes the the helper
object, after calling its clear() method to prevent it from
logging.
如果您想查看/处理异常,只需使用add_done_callback
处理任务的结果,并在获得异常时执行所有必要的操作:
add_done_callback
谢谢,@ dano。这是import asyncio
def handle_result(fut):
if fut.exception():
fut.result() # This will raise the exception.
def schedule_something():
global f
tsk = asyncio.async(do_something())
tsk.add_done_callback(handle_result)
f = tsk
@asyncio.coroutine
def do_something():
raise Exception()
loop = asyncio.get_event_loop()
loop.call_soon(schedule_something)
loop.run_forever()
loop.close()
的替代品,它可以自动执行此操作-
asyncio.create_task
给定示例的更新版本-
def create_task(coro):
def on_task_done(fut):
if fut.exception():
fut.result()
async def task_wrapper():
task.remove_done_callback(on_task_done)
return await task
task = asyncio.create_task(coro)
task.add_done_callback(on_task_done)
return task_wrapper()
async def do_something():
raise Exception()
async def schedule_something():
global f
tsk = create_task(do_something())
f = tsk # If this line is commented out, exceptions can be heard.
asyncio.run(schedule_something())