Python Async Programming — asyncio, await, and Coroutines

Master asynchronous Python: asyncio, coroutines, tasks, gathering, and building concurrent applications.

Python Advanced 📖 11 min

Coroutines and await

import asyncio

async def fetch_data(url):
    print(f'Fetching {url}...')
    await asyncio.sleep(1)  # Simulate async I/O
    return f'Data from {url}'

# Running a coroutine
result = asyncio.run(fetch_data('https://api.example.com'))

Gathering Multiple Tasks

async def main():
    # Run concurrently
    results = await asyncio.gather(
        fetch_data('https://api.example.com/users'),
        fetch_data('https://api.example.com/posts'),
        fetch_data('https://api.example.com/comments')
    )
    return results

asyncio.run(main())

Task Management

async def background_task(name):
    while True:
        print(f'{name}: working...')
        await asyncio.sleep(2)

async def main():
    # Create background tasks
    task1 = asyncio.create_task(background_task('Worker1'))
    task2 = asyncio.create_task(background_task('Worker2'))
    
    # Do other work
    await asyncio.sleep(5)
    
    # Cancel tasks
    task1.cancel()
    task2.cancel()

asyncio.run(main())

Semaphore for Rate Limiting

async def fetch_with_limit(urls, max_concurrent=5):
    semaphore = asyncio.Semaphore(max_concurrent)
    
    async def fetch(url):
        async with semaphore:
            return await fetch_data(url)
    
    tasks = [fetch(url) for url in urls]
    return await asyncio.gather(*tasks)

Ready to Level Up Your Coding Skills?

Join Coderz Club and learn to code with challenges, battles, and an AI mentor.

Join Coderz Club Free →