If it's not an async
function, then you don't need to await
it obviously. Not every function you call inside an async
function must be async
nor must be await
ed; you can call regular non-async functions from within an async
function.
The entire asyncio model works around an event loop. Only one task can run at any one time, and the event loop coordinates what is currently running. An await
inside a function suspends the execution of that function and allows another task to be run on the event loop. So, in this example:
async def caller():
await bar()
print('finish')
The execution goes like this:
caller()
is called and scheduled on the event loop, which will execute it as soon as an availability exists.
- It calls
bar()
, which schedules its execution on the event loop.
- The
await
suspends the execution of caller
.
- The event loop executes
bar
; let's say it's making a network request, so nothing will happen until that response returns, the event loop is free to run any other scheduled async tasks…
- The network response returns, the event loop resumes the execution of
bar
.
bar
ends, the event loop resumes the execution of caller
.
await
exists to coordinate the sequence in which asynchronous tasks are run and what task depends on the result of what other task.