Lesson 42 of 55
PythonAsyncIO Basics
20 minutes
Course progress76%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 42 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
asyncio provides the building blocks for asynchronous, non‑blocking code using the async/await syntax. It shines for I/O‑bound workloads like network calls.
import asyncio
async def greet(name):
await asyncio.sleep(1) # simulate async I/O
return f'Hello, {name}!'
async def main():
messages = await asyncio.gather(greet('Alice'), greet('Bob'))
for m in messages:
print(m)
asyncio.run(main())
The event loop (asyncio.run) schedules coroutines and runs them concurrently.