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?
#2
In the documentation it's described as:

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.

Explained with my own words: If a awaitable raises an error, you won't get results back, the tasks are not cancelled, but the caller have to catch the error.

Quote:If return_exceptions is True, exceptions are treated the same as successful results, and aggregated in the result list.


Here both versions explained with code:
import asyncio, time


async def fn1(x):
    await asyncio.sleep(3)
    print(f"fn1 is called with x={x}")
    return f"Hello from fn1: {x}"


async def raise_error(y):
    await asyncio.sleep(2)
    print(f"raise_error is called with y={y}")
    raise asyncio.TimeoutError() # <- function exits here
    # no result
    return f"Hello from raise_error"


async def main():
    print("start:",time.ctime())
    print("return_exceptions=False")
    try:
        result1 = await asyncio.gather(
            fn1("First call"), # <- 1st result
            raise_error("Second call"), # <- here is the error
            fn1("Third call"), # <- 3rd result
            return_exceptions=False, # <- don't return result, if an exception was raised
            # but tasks are not canceled. You can see it with the print function, that
            # fn1 is still called, but you won't get a return value from gather
            )
    except Exception as e:
        print("Catched Exception from gather")
        print("We don't get the result back, but the tasks are not canceled")
    
    # result1 does not exist
    try:
        print('Result1', result1)
    except NameError:
        print('As expected you get a NameError, result1 does not exist')
    
    print()
    print('Sleeping 2 seconds')
    await asyncio.sleep(2)
    print()
    print('return_exceptions=True')
    
    result2 = await asyncio.gather(
            fn1("First call"), # <- 1st result
            raise_error("Second call"), # <- here is the error
            fn1("Third call"), # <- 3rd result will evaluated aswell
            return_exceptions=True, # <- return exceptions as results
        )
    print("result2:", result2)
    await asyncio.sleep(2)
    print("end:",time.ctime())
    


asyncio.run(main())
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How to understand asyncio.gather return_exceptions=False? - by DeaD_EyE - Sep-19-2019, 12:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to gather specific second-level items from a list chatguy 2 1,668 Dec-17-2021, 05:05 PM
Last Post: chatguy
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,339 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  How to gather information from remote host using ansible module in python amritjsr 0 2,125 Jul-20-2019, 01:17 AM
Last Post: amritjsr
  Trying to understand blocking in both multitasking and asyncio penright 7 4,937 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