Today I learned about the behaviour of asyncio.gather.

asyncio.gather

Today I learned that you can use asyncio.gather to gather the return results of a bunch of awaitable objects. The list of the returns come in the order the awaitables are passed in to gather, and not in the order they are completed:

import asyncio

async def t1():
    await asyncio.sleep(1)
    return 1

async def t2():
    await asyncio.sleep(3)
    return 2

async def t3():
    await asyncio.sleep(2)
    return 3

async def main():
    return await asyncio.gather(t1(), t2(), t3())

print(asyncio.run(main()))  # [1, 2, 3]

Context: this tiny TIL article was brought to you thanks to a rant by Tushar on X (context: https://x.com/tusharisanerd/status/1853273489794302320).

Become a better Python 🐍 developer, drop by drop πŸ’§

Get a daily drop of Python knowledge. A short, effective tip to start writing better Python code: more idiomatic, more effective, more efficient, with fewer bugs. Subscribe here.

References

Previous Post Next Post

Blog Comments powered by Disqus.