Message345861
asyncio.gather doesn't handle custom exception exceptions that inherit from BaseException in the same manner that it handles those that inherit from Exception, regardless of whether return_exceptions is set to True or False.
In the example below, I am using return_exceptions=True. If the custom exception inherits from Exception, a list printed, as expected. Conversely, if the custom exception inherits from BaseException, it is propagated:
import asyncio
class CustomException(BaseException): # It works if base class changed to Exception
pass
async def do_this(x):
if x == 5:
raise CustomException()
await asyncio.sleep(1)
print(f'THIS DONE: {x}')
async def main():
print('BEGIN')
tasks = [do_this(x) for x in range(1, 11)]
result = await asyncio.gather(*tasks, return_exceptions=True)
print(f'Result: {result}')
print('END')
asyncio.run(main()) |
|
Date |
User |
Action |
Args |
2019-06-17 14:10:41 | cmermingas | set | recipients:
+ cmermingas |
2019-06-17 14:10:41 | cmermingas | set | messageid: <1560780641.84.0.00364750397427.issue37317@roundup.psfhosted.org> |
2019-06-17 14:10:41 | cmermingas | link | issue37317 messages |
2019-06-17 14:10:41 | cmermingas | create | |
|