Python - asyncio - 从未检索到任务异常

标签 python asynchronous python-asyncio coroutine

说明:(简体)

  • 我有2个任务。
  • 在每个任务中,我有 3 个协程。
  • 第一个任务的 2 个协程失败。 (模拟)
  • 在处理任务结果时,我收到一条“从未检索到任务异常”消息。
  • 我相信这是因为仅处理了该任务中两个失败的协程之一的异常。
  • 如何处理任务中两个协程的异常和/或避免“从未检索到任务异常”消息?

  • 代码:(简体)
    import asyncio
    async def download(data):
        filename = "*" if data in ["b", "c"] else data  # simulated failure
        with open(filename, "w") as f:
            f.write(data)
    async def coro(data_list):
        coroutines = [download(data) for data in data_list]
        for coroutine in asyncio.as_completed(coroutines):
            await coroutine
    async def main():
        task1 = asyncio.create_task(coro(["a", "b", "c"]))
        task2 = asyncio.create_task(coro(["d", "e", "f"]))
        results = await asyncio.gather(task1, task2, return_exceptions=True)
        for _ in results:
            pass
    asyncio.run(main())
    
    输出:(简化)
    Task exception was never retrieved
    future: <Task finished coro=<download() done, defined at D:/myscript.py:2> exception=OSError(22, 'Invalid argument')>
    Traceback (most recent call last):
      File "D:/myscript.py", line 4, in download
        with open(filename, "w") as f:
    OSError: [Errno 22] Invalid argument: '*'
    

    最佳答案

    如果你想收集异常而不是引发它们,你可以使用 asyncio.gather(return_exceptions=True)coro以及。例如:

    import asyncio
    
    async def download(data):
        if data in ['b', 'c']:
            1/0    # simulate error
        return 42  # success
    
    async def coro(data_list):
        coroutines = [download(data) for data in data_list]
        return await asyncio.gather(*coroutines, return_exceptions=True)
    
    async def main():
        task1 = asyncio.create_task(coro(["a", "b", "c"]))
        task2 = asyncio.create_task(coro(["d", "e", "f"]))
        return await asyncio.gather(task1, task2, return_exceptions=True)
    
    print(asyncio.run(main()))
    
    这将打印:
    [[42, ZeroDivisionError('division by zero'), ZeroDivisionError('division by zero')], [42, 42, 42]]
    

    关于Python - asyncio - 从未检索到任务异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65147823/

    相关文章:

    python - IteratorGetNext 上的 TensorFlow 性能瓶颈

    python - 如何使用正则表达式选择特定扩展名的文件路径

    wcf - 如何使用 Moq 模拟 WCF 客户端?

    python - 如何正确使用 asyncio.FIRST_COMPLETED

    python - 检查输入 : expected lstm_1_input to have 3 dimensions, 时出错,但仅在 epoch>1 和特定数据集拆分时得到形状为 (0, 1) 的数组

    python - 使 FastAPI WebSockets 的 CPU 密集型任务异步

    c# - 有什么理由立即使用 await 和 async 吗?

    pytest - 同时运行测试

    python - 异步等待方法在 Python 3 中完成

    python - 奇怪的 `UnicodeEncodeError` 使用 `os.path.exists`