Python Forum
How to understand asyncio.gather return_exceptions=False?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to understand asyncio.gather return_exceptions=False?
#3
Thanks for your helping!

I have ran your code.

Firstly, if set

 asyncio.gather(*aws, loop = None, return_exceptions = True) 
then we can get the Exception as an object in the result list. that's a good choice to know which awaitable task or coroutine had an exception after asyncio running.

Secondly, if set

 asyncio.gather(*aws, loop = None, return_exceptions = False) 
after the asyncio.TimeoutError() raised, "fn1 is called with First call" and "fn1 is called with Third call" both sentences didn't print yet.

terminal's output information is as below:
Output:
#nothing above start: Thu Sep 19 21:24:37 2019 return_exceptions=False raise_error is called with y=Second call Catched Exception from gather We don't get the result back, but the tasks are not canceled As expected you get a NameError, result1 does not exist # nothing below
what makes me confused is on my understanding about the doc

Quote:"If return_exceptions is False (default), the first raised exception is immediately propagated to the task that awaits on gather(). Other awaitables in the aws sequence won’t be cancelled and will continue to run."

https://docs.python.org/3/library/asynci...ncurrently

if there is an Exception in one of the awaitable tasks or coroutines, the others awatiable tasks will still run. In the examples you gave,
 
async def fn1(x):
    await asyncio.sleep(3)
    print(f"fn1 is called with x={x}") # from the doc says, I think this sentence will still run
    return f"Hello from fn1: {x}" 

the print sentence will be running and give a printing on terminal. but nothing happended in the termial.
Reply


Messages In This Thread
RE: How to understand asyncio.gather return_exceptions=False? - by learnpython - Sep-20-2019, 08:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to gather specific second-level items from a list chatguy 2 1,666 Dec-17-2021, 05:05 PM
Last Post: chatguy
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,336 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  How to gather information from remote host using ansible module in python amritjsr 0 2,122 Jul-20-2019, 01:17 AM
Last Post: amritjsr
  Trying to understand blocking in both multitasking and asyncio penright 7 4,929 Jun-29-2018, 04:22 PM
Last Post: penright

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020