Обложка канала

Библиотека Python разработчика

20835 @BookPython

Библиотека Python разработчика. Книги по программированию на Python.

Библиотека Python разработчика

4 года назад
Открыть в
Dealing with exceptions in asynchronous programs may be not a simple task. In asyncio, if coroutine raises an exception, it's then propagated to the code that awaits the corresponding future. If multiple places do await, every one of them gets the exception (since it's stored in the exception). The following code prints error five times: import asyncio async def error(): await asyncio.sleep(1) raise ValueError() async def waiter(task): try: await task except ValueError: print('error') else: print('OK') async def main(): task = asyncio.get_event_loop().create_task(error()) for _ in range(5): asyncio.get_event_loop().create_task(waiter(task)) await asyncio.sleep(2) loop = asyncio.get_event_loop() loop.run_until_complete(main()) If an exception is raised, but the task is never awaited, the exception is lost. In that case, when the task is destroyed, it warns you with “Task exception was never retrieved” message. When you use await asyncio.gather(tasks) and one of the tasks raises an exception, it is propagated to you. However, if multiple tasks raise exceptions, you still only get the first one, the others are silently lost: import asyncio async def error(i): await asyncio.sleep(1) raise ValueError(i) async def main(): try: await asyncio.gather( error(1), error(2), error(3), ) except ValueError as e: print(e) loop = asyncio.get_event_loop() loop.run_until_complete(main()) You may use gather with return_exceptions=True that make it return exceptions as though they are regular result values. The following code prints [42, ValueError(2,), ValueError(3,)]: import asyncio async def error(i): await asyncio.sleep(1) if i > 1: raise ValueError(i) return 42 async def main(): results = await asyncio.gather( error(1), error(2), error(3), return_exceptions=True, ) print(results) loop = asyncio.get_event_loop() loop.run_until_complete(main())