Coroutines and Async/Await

Advanced PythonAsync ProgrammingFree Lesson

Advertisement

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

  1. Fetch data from multiple URLs concurrently
  2. Implement timeout for coroutine
  3. Use asyncio.wait() with tasks
  4. Create async context manager
  5. Handle exceptions in async code

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement