python - 使用 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 中?
解决方案
推荐阅读
- mode - 如何制作夜间模式?
- java - 单元测试不能通过
- javascript - 尽快:恶意软件清除 javascript
- javascript - 在 POST 到服务器之前将捕获的 getUserMedia 视频保存到文件
- python - 如何修复'NameError:名称'russ_yaz'未定义'
- c# - ASP.NET MVC 应用程序中的登录过程总是失败
- wordpress - Wordpress 查询在 wp_query 中添加 0=1 并且结果消失
- html - 在网页中创建一个链接,允许通过加载指定的 kml 文件打开谷歌地球和/或谷歌地图
- prolog - 打印列表的元素,但以不同方式处理最后一个元素
- java - Springboot 容器使用 docker-compose 连接到 mongo 容器的 503 错误代码