Python Series #1 Concurrency in Python using Asyncio

Lesson 1: Introduction to Concurrency and Asyncio

Concurrency is the concept of executing multiple tasks in parallel, making better use of available resources. Python's asyncio library enables writing asynchronous, concurrent code using the async/await syntax.

Asyncio is a part of the Python standard library, starting from Python 3.4. It enables efficient handling of multiple IO-bound tasks without the need for threads.

Lesson 2: Async/Await and Coroutine Basics

To create a coroutine, use the async def keyword before the function definition. You can then call the coroutine using the await keyword. Here's an example:

import asyncio

async def hello_world():
    print("Hello, World!")

async def main():
    await hello_world()

Lesson 3: Asyncio Tasks and Running Coroutines Concurrently

To run coroutines concurrently, you can create tasks. Tasks are asyncio's objects that wrap coroutines and run them concurrently when awaited:

import asyncio

async def hello(name, delay):
    await asyncio.sleep(delay)
    print(f"Hello, {name}!")

async def main():
    task1 = asyncio.create_task(hello("Alice", 1))
    task2 = asyncio.create_task(hello("Bob", 2))

    await task1
    await task2

Lesson 4: Asyncio Gather and Wait Functions

asyncio.gather is a high-level function that schedules multiple coroutines concurrently and returns their results as a list.
asyncio.wait is another high-level function that schedules coroutines concurrently and returns two sets of done and pending tasks:

import asyncio

async def square(x):
    await asyncio.sleep(x)
    return x * x

async def main():
    results = await asyncio.gather(square(1), square(2), square(3))

    done, pending = await asyncio.wait({square(4), square(5), square(6)})
    for task in done:

Lesson 5: A Real-World Example with Asyncio

Let's say you want to fetch multiple URLs concurrently using aiohttp, an asynchronous HTTP client for asyncio. Here's an example:

import asyncio
import aiohttp

async def fetch(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            return await response.text()

async def main():
    urls = ['', '', '']
    tasks = [asyncio.create_task(fetch(url)) for url in urls]
    responses = await asyncio.gather(*tasks)

    for response in responses:

This course should give you a basic understanding of concurrency and how to implement it using asyncio. To learn more, you can refer to the official asyncio documentation: