async

KeywordPython 3.5+Advanced

Declares an asynchronous coroutine function or context manager

Quick Info

Documentation
Official Docs
Python Version
3.5+

Learn by Difficulty

Quick Example

python
import asyncio

async def greet(name):
    print(f"Hello, {name}!")
    return f"Greeted {name}"

# Run the async function
result = asyncio.run(greet("Python"))
print(f"Result: {result}")

# Async with await
async def fetch_data():
    print("Fetching...")
    await asyncio.sleep(0)  # simulate async work
    print("Done!")
    return [1, 2, 3]

data = asyncio.run(fetch_data())
print(f"Data: {data}")

async def creates a coroutine function. Calling it returns a coroutine object that must be awaited or run with asyncio.run().

Try in Playground

Tags

languagesyntaxcoreasyncconcurrency

Related Items