When should you use async and when should you use await?
async , for example, defines an asynchronous function
async def f(): return 5
Or indicates that
for asynchronous, i.e. gets values from an asynchronous generator
async def generator(): yield 1 yield 2 yield 3 async def main(): async for x in generator(): print(x)
Or allows you to work with an asynchronous version of the context manager
async with await asyncio.start_server(callback, '127.0.0.1', 8080) as server: await server.serve_forever()
await waits for the
awaitable object to complete and returns the result.
async main(): print(await f())
It is difficult to confuse them:
async is part of a statement,
await is a unary operator, part of an expression
async for and
async with should only be used inside an asynchronous function.