首页 > 解决方案 > 使用 asyncio.gather 正确处理 SystemExit

问题描述

我想运行带有一些等待的 asyncio.gather,这可能会引发 SystemExit。

import asyncio


async def func(t):
    await asyncio.sleep(t)
    raise SystemExit('Err')


async def main():
    tasks = [asyncio.ensure_future(func(t)) for t in range(0, 3)]
    print('Starting gather')
    try:
        await asyncio.gather(*tasks, return_exceptions=True)
    except BaseException:
        pass
    print('Gather returned')
    for t in tasks:
        if not t.done():
            t.cancel()
        try:
            await t
        except BaseException:
            pass
    await asyncio.sleep(1)


asyncio.run(main())

该片段的退出代码为 1,因为 asnycio.run 将在其内部的 cancel_all_tasks 中再次引发 SystemExit,它本身调用 tasks.gather。如何防止这种行为,所以代码段的退出代码为 0(不从 asyncio.run 捕获异常)。

有没有办法防止 cancel_all_tasks 看到我已经取消的任务?是否有另一种解决方案,然后将 SystemExit 包装在 func 中?

标签: pythonpython-asynciosystemexit

解决方案


推荐阅读