Python async Keyword
Example
Define an asynchronous function and run it:
import asyncio
async def greet():
return "Hello"
async def main():
msg = await greet()
print(msg)
asyncio.run(main())
Try it Yourself »
Definition and Usage
The async
keyword declares a function as asynchronous (a coroutine), allowing use of await
inside it.
Asynchronous functions run within an event loop (for example using asyncio.run()
).
More Examples
Example
Await inside an async
function:
import asyncio
async def work():
await asyncio.sleep(0)
print("Done!")
asyncio.run(work())
Try it Yourself »
Related Pages
The await
keyword.
The with
keyword, used for exception handling and resource management.