Using await
does NOT make the call synchronous. It is just syntactic sugar to make it look like "normal" sequential code. But while the result of the function call is await
ed the event loop still continues in the background.
For example the following code executes foo()
twice which will await
a sleep
. But even though it uses await
the second function invokation will execute before the first one finishes. Ie. it runs in parallel.
import asyncio
async def main():
print('started')
await asyncio.gather(
foo(),
foo(),
)
async def foo():
print('starting foo')
await asyncio.sleep(0.1)
print('foo finished.')
asyncio.run(main())
prints:
started
starting foo
starting foo
foo finished.
foo finished.