Skip to main content
Editor

Python Async/Await: Write Non-Blocking Code from Scratch

Expert30 min6 exercises120 XP
0/6 exercises

Imagine you're a chef in a busy kitchen. You don't stand and watch the oven for 20 minutes while your pasta boils on another burner. Instead, you start the oven, move to the pasta, then check the salad -- all without waiting idle. That's exactly what asynchronous programming does for your code.

What Is Asynchronous Programming?

In regular (synchronous) Python, each line runs one after the other. If one line takes 5 seconds -- like downloading a file -- everything else just waits. Asynchronous programming lets your program start a slow task, move on to other work, and come back when the slow task finishes.

Python's async/await syntax, introduced in Python 3.5, gives you a clean way to write non-blocking code. It's built on top of the asyncio library, which provides the event loop that coordinates all the concurrent tasks.


Coroutines: The Building Blocks

A coroutine is a special function declared with async def. When you call it, it doesn't run immediately. Instead, it returns a coroutine object -- a promise that the function will run when you schedule it.

Python
import asyncio
async def greet(name):
return f'Hello, {name}!'
# Calling a coroutine function only creates a coroutine object
coro = greet('Alice')
print(type(coro))
# await runs it and hands back the result
result = await coro
print(result)
Output
<class 'coroutine'>
Hello, Alice!

The await Keyword

The await keyword pauses the current coroutine until the awaited coroutine finishes. In a script you can only use await inside an async def function; this page also allows it at the top level.

Python
import asyncio
async def fetch_data():
await asyncio.sleep(0.5) # stands in for a network call
return {'users': ['Alice', 'Bob']}
async def process():
data = await fetch_data() # pauses here until fetch_data finishes
print(f'Got {len(data["users"])} users')
return data
data = await process()
Output
Got 2 users

Running Coroutines with asyncio.run()

asyncio.run() is the entry point of an async script. It creates an event loop, runs your top-level coroutine, and shuts everything down cleanly when done. On this page the loop is already running, so the last line awaits main() directly.

Python
import asyncio
async def main():
print('Starting...')
await asyncio.sleep(1)
print('Done!')
# In a .py script on your computer:
# asyncio.run(main())
# Here an event loop is already running, so:
await main()
Output
Starting...
Done!

Concurrent Tasks with asyncio.gather()

The real power of async shows up when you run multiple tasks at once. asyncio.gather() takes multiple coroutines and runs them concurrently. It's like putting three dishes in the oven at the same time instead of cooking them one by one.

Sequential (slow)
# Takes 3 seconds total
async def main():
    a = await fetch_user(1)   # 1 sec
    b = await fetch_user(2)   # 1 sec
    c = await fetch_user(3)   # 1 sec
    return [a, b, c]
Concurrent (fast)
# Takes ~1 second total
async def main():
    a, b, c = await asyncio.gather(
        fetch_user(1),
        fetch_user(2),
        fetch_user(3),
    )
    return [a, b, c]

Run both versions and time them:

Python
import asyncio
import time
async def fetch_user(user_id):
await asyncio.sleep(0.5) # a network call that takes half a second
return f'User_{user_id}'
start = time.time()
users = []
for i in range(1, 4):
users.append(await fetch_user(i))
print(f'One after another: {time.time() - start:.1f}s -> {users}')
start = time.time()
users = await asyncio.gather(fetch_user(1), fetch_user(2), fetch_user(3))
print(f'With gather(): {time.time() - start:.1f}s -> {users}')
Output
One after another: 1.5s -> ['User_1', 'User_2', 'User_3']
With gather():     0.5s -> ['User_1', 'User_2', 'User_3']

Async For and Async Context Managers

Python also provides async for to iterate over asynchronous iterators and async with for asynchronous context managers. These are useful when each iteration or resource setup involves I/O.

Python
import asyncio
class AsyncCounter:
'''An async iterator: each value arrives after a short wait.'''
def __init__(self, stop):
self.current = 0
self.stop = stop
def __aiter__(self):
return self
async def __anext__(self):
if self.current >= self.stop:
raise StopAsyncIteration
await asyncio.sleep(0.2) # waiting for the next value
self.current += 1
return self.current
class Connection:
'''An async context manager: opening and closing can await too.'''
async def __aenter__(self):
await asyncio.sleep(0.1)
print('Connected')
return self
async def __aexit__(self, exc_type, exc, tb):
print('Closed')
async with Connection():
async for number in AsyncCounter(3):
print('Got', number)
Output
Connected
Got 1
Got 2
Got 3
Closed

The Event Loop: Orchestrating It All

The event loop is the conductor of the async orchestra. It keeps track of all running tasks, decides which one to wake up next, and handles I/O notifications from the operating system. You rarely touch the loop directly, but two of its tools come up all the time: create_task() runs a coroutine in the background, and wait_for() puts a time limit on one.

Python
import asyncio
async def tick(name, delay):
await asyncio.sleep(delay)
print(f'{name} finished after {delay}s')
return name
# create_task() starts a coroutine while you carry on with other work
task = asyncio.create_task(tick('background', 0.3))
print('Task started, doing other work...')
await asyncio.sleep(0.1)
print('Still working')
print('Result:', await task)
# wait_for() gives up after a timeout and cancels the coroutine
try:
await asyncio.wait_for(tick('slow', 2), timeout=0.5)
except TimeoutError:
print('slow took too long and was cancelled')
Output
Task started, doing other work...
Still working
background finished after 0.3s
Result: background
slow took too long and was cancelled

Practice Exercises

Define a Coroutine
Write Code

Write an async def function called greet that takes a name parameter and returns the string Hello, <name>! Welcome to async Python. (where <name> is replaced with the argument). Then, outside the function, print the type of the object returned by calling greet('World') (do NOT await it).

# Define an async function called greet
Predict the Coroutine Output
Predict Output

What does the following code print?

import asyncio

async def add(a, b):
    return a + b

result = add(3, 4)
print(type(result).__name__)
# Type what you think the output will be, then run to check
import asyncio
async def add(a, b):
return a + b
result = add(3, 4)
print(type(result).__name__)
Build an Async Iterator
Write Code

Create a class called Countdown that implements the async iterator protocol. It should:

1. Accept a start value in __init__

2. Implement __aiter__ returning self

3. Implement __anext__ that counts down from start to 1, raising StopAsyncIteration when done

After the class, print the string Countdown ready to confirm your class is defined.

# Create the Countdown async iterator class
Fix the Async Bug
Fix the Bug

The code below tries to define an async context manager but has two bugs. Fix them so that async with AsyncResource(): works and a block inside it shows:

Entering context
Doing work
Exiting context

Keep the last line, which prints Context manager ready.

class AsyncResource:
def __aenter__(self):
print('Entering context')
return self
def __aexit__(self, exc_type, exc_val, exc_tb):
print('Exiting context')
return False
print('Context manager ready')
Gather Concurrent Results
Write Code

Write an async def function called double that takes x, waits 0.1 seconds with asyncio.sleep, and returns x * 2. Then run double(1), double(2) and double(3) at the same time with asyncio.gather() and print the list of results.

import asyncio
# Write double, then gather three calls and print the results
Refactor to Async Pattern
Refactor

The synchronous code below processes users sequentially. Refactor it to use async def syntax that *would* run concurrently with asyncio.gather(). Keep the same logic but:

1. Make fetch_user an async def

2. Make process_all an async def that uses await on each fetch_user call

3. After the definitions, print Refactored to async to confirm.

def fetch_user(user_id):
return {'id': user_id, 'name': f'User_{user_id}'}
def process_all(ids):
results = []
for uid in ids:
user = fetch_user(uid)
results.append(user)
return results
print(process_all([1, 2, 3]))

Related Tutorials

Was this tutorial helpful?