Suppose there are several coroutines that we send to the event loop
task_list = [task1(), task2(), task3()]
res = asyncio.gather(*task_list, return_exceptions=True)
Regarding documentation
If return_exceptions is True, exceptions are treated the same as successful results, and aggregated in the result list.
I.e. if for example task2 returns an error, the result will be
res = [task1_result, task2_error, task3_result]
The result is further processed in another process, and I would like to pass some default value if there is an error in the response. The question is how to check for an exception in the event_loop response. Smth like (warning pseudo code!)
response = default if any(res is Error) else res
You could use map
function to replace errors in result list by default value. For checking if result is an error isinstance(result, Exception)
can be used. Like so:
task_list = [task1(), task2()]
res = await asyncio.gather(*task_list, return_exceptions=True) # res = ["result", ArithmeticError()]
res = list(map(lambda r: r if not isinstance(r, Exception) else default_value, res)) # res = ["result", None]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With