Introduction
Asynchronous programming enables concurrent execution without threading, using coroutines.
Basic Coroutine
async def fetch_data():
print("Fetching data...")
await asyncio.sleep(2) # Simulate I/O
return {"data": [1, 2, 3]}
# Run coroutine
async def main():
result = await fetch_data()
print(result)
asyncio.run(main())
Running Multiple Coroutines
async def task(name, delay):
print(f"{name} starting")
await asyncio.sleep(delay)
print(f"{name} finished")
return name
async def main():
# Sequential
result1 = await task("A", 1)
# Concurrent
results = await asyncio.gather(
task("A", 1),
task("B", 2),
task("C", 0.5)
)
print(results) # ['A', 'B', 'C']
Tasks and Futures
async def main():
task = asyncio.create_task(fetch_data())
# Do other work
result = await task
# Wait for multiple
tasks = [asyncio.create_task(task(i)) for i in range(5)]
await asyncio.gather(*tasks)
Practice Problems
- Fetch data from multiple URLs concurrently
- Implement timeout for coroutine
- Use asyncio.wait() with tasks
- Create async context manager
- Handle exceptions in async code