---
title: "Python Asyncio Tutorial: Async/Await, Tasks & Concurrency Guide"
description: "Learn how Python's asyncio library handles concurrent I/O operations efficiently. This guide covers coroutines, event loops, tasks, the difference between await and create_task, async context managers, and practical examples including a chat server implementation."
image: "https://www.sethserver.com/static/images/asynchronous-programming-with-python-boosting-performance-and-scalability-1-1.webp"
---

# Python Asyncio Tutorial: Async/Await, Tasks & Concurrency Guide

You know that feeling where your program is "working," but it's also just... waiting? Waiting on the network. Waiting on disk. Waiting on a database that's thinking real hard about returning three rows. That's where `asyncio` earns its keep.

## The basic idea: stop standing around

Synchronous code is polite to a fault. Task A waits. Task B waits. Your CPU sits there doing nothing while the network thinks about life.

Async code doesn't wait. It starts task A, hits an `await`, switches to task B. Same thread. Less time wasted.

Here's the smallest example that actually shows the behavior:

```python
import asyncio

async def greet(name: str):
    print(f"hi {name}")
    await asyncio.sleep(1)
    print(f"bye {name}")

async def main():
    await asyncio.gather(
        greet("ada"),
        greet("grace"),
        greet("linus"),
    )

asyncio.run(main())
```

All three say "hi" right away, then you wait one second, then all three say "bye." That one second covers all three greetings. If you expected three seconds, welcome to async.

### Vocabulary you need

- **Coroutine**: an `async def` function. It can pause at `await`.
- **Event loop**: the scheduler that decides what runs next.
- **Task**: a coroutine that's been scheduled to run.

## `await` vs `create_task`: the mistake everyone makes

If you `await` three network calls in a row, you're back to waiting in line. The fix is to start them first, then wait for all of them.

```python
import asyncio

async def fetch_data(url: str):
    await asyncio.sleep(2)  # pretend this is the network
    return f"data from {url}"

async def main():
    urls = [
        "https://api.example.com/users",
        "https://api.example.com/products",
        "https://api.example.com/orders",
    ]

    tasks = [asyncio.create_task(fetch_data(u)) for u in urls]
    results = await asyncio.gather(*tasks)

    for r in results:
        print(r)

asyncio.run(main())
```

This is the pattern I reach for when I have independent I/O. Three API calls. Ten database queries. A pile of files to read. Start them, then gather.

## Manual loop control (rare, but real)

Most of the time, `asyncio.run()` is the right answer. Sometimes you're integrating with something else and you need more control.

```python
import asyncio

async def countdown(n: int):
    while n:
        print(n)
        await asyncio.sleep(1)
        n -= 1

loop = asyncio.get_event_loop()
try:
    loop.create_task(countdown(3))
    loop.run_until_complete(asyncio.sleep(4))
finally:
    loop.close()
```

I don't do this daily, but it's useful to recognize it when you're reading other people's code. Which you will. A lot.

## A tiny chat server that shows why async exists

Async works well when you have many clients doing small reads/writes. A chat server is the cleanest demo.

```python
import asyncio

class ChatServer:
    def __init__(self):
        self.clients = set()

    async def handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
        self.clients.add(writer)
        addr = writer.get_extra_info("peername")
        print(f"connected: {addr}")

        try:
            while True:
                data = await reader.read(100)
                if not data:
                    break

                msg = data.decode().strip()
                for client in list(self.clients):
                    if client is writer:
                        continue
                    client.write((msg + "\n").encode())
                    await client.drain()
        finally:
            self.clients.discard(writer)
            writer.close()
            await writer.wait_closed()
            print(f"disconnected: {addr}")

async def main():
    server = ChatServer()
    srv = await asyncio.start_server(server.handle_client, "127.0.0.1", 8888)
    async with srv:
        await srv.serve_forever()

asyncio.run(main())
```

One coroutine per client. No thread-per-connection nonsense. The event loop switches between them.

## `async with`: you still need to clean up resources in async code

Resource management doesn't go away just because you added `await`.

```python
import asyncio

class AsyncDatabaseConnection:
    async def __aenter__(self):
        await asyncio.sleep(1)  # connect
        return self

    async def __aexit__(self, exc_type, exc, tb):
        await asyncio.sleep(0.5)  # disconnect

    async def query(self, sql: str):
        await asyncio.sleep(0.5)
        return f"result for {sql}"

async def main():
    async with AsyncDatabaseConnection() as db:
        print(await db.query("select * from users"))

asyncio.run(main())
```

This pattern keeps your setup/teardown clean, even when exceptions show up.

## Rules

- If you call blocking code inside async, you're lying to the event loop.
- Use `create_task()` to run things concurrently. Use `await` to wait for a result.
- Use `asyncio.Lock` if multiple tasks touch shared state.
- Handle exceptions inside coroutines. Unhandled task exceptions are a fun surprise.
- Don't use asyncio for CPU-bound work. If you're crunching numbers, use multiprocessing or offload to threads.

Asyncio isn't hard. The hard part is noticing where you're accidentally blocking. Use a profiler. You'll find them.

For more practical patterns like handling timeouts, worker queues, and mixing sync code with async, check out these [python asyncio Recipes](https://www.sethserver.com/python/python-asyncio-recipes.html).

-Sethers


```json
{"@context": "https://schema.org", "@type": "BlogPosting", "headline": "Python Asyncio Tutorial: Async/Await, Tasks & Concurrency Guide", "datePublished": "2024-09-30T00:49:02", "dateModified": "2026-03-18T23:37:14", "image": {"@type": "ImageObject", "url": "https://www.sethserver.com/static/images/asynchronous-programming-with-python-boosting-performance-and-scalability-1-1.webp", "width": 400, "height": 400}, "url": "https://www.sethserver.com/programming/asynchronous-programming-with-python-boosting-performance-and-scalability.html", "author": {"@type": "Person", "name": "Seth Black", "url": "https://www.linkedin.com/in/seth-black-tx"}, "publisher": {"@type": "Organization", "name": "Sethserver", "logo": {"@type": "ImageObject", "url": "https://www.sethserver.com/static/images/PaperAirplane512.png"}}, "description": "Learn how Python's asyncio library handles concurrent I/O operations efficiently. This guide covers coroutines, event loops, tasks, the difference between await and create_task, async context managers, and practical examples including a chat server implementation.", "articleSection": "Technology", "articleBody": "In the ever-evolving landscape of software development, the ability to handle multiple tasks concurrently has become increasingly crucial. As applications grow in complexity and user bases expand, developers need tools that can efficiently manage thousands of simultaneous operations without breaking a sweat. Enter asynchronous programming, a paradigm that has revolutionized how we approach concurrency in Python.  In this post, we'll dive deep into asynchronous programming using Python's asyncio library. We'll explore how this powerful tool can dramatically improve the performance and scalability of your applications, particularly when dealing with I/O-bound operations. By the end of this journey, you'll have a solid grasp of asyncio's core concepts and be well-equipped to leverage its capabilities in your own projects.  Understanding Asynchronous Programming  Before we delve into the specifics of asyncio, let's take a moment to understand what asynchronous programming is and how it differs from traditional synchronous code.  In synchronous programming, tasks are executed sequentially. Each operation must complete before the next one begins. This approach is straightforward and easy to reason about, but it can lead to inefficiencies, especially when dealing with I/O-bound operations that involve waiting for external resources.  Asynchronous programming, on the other hand, allows multiple operations to progress concurrently. Instead of waiting idly for a slow operation to complete, the program can switch to another task, maximizing CPU utilization and improving overall performance.  Enter asyncio  Python's asyncio library provides a foundation for writing asynchronous code using the async/await syntax. It introduces several key components:  1. Coroutines: Special functions that can be paused and resumed. 2. Event Loop: The core of asyncio, responsible for scheduling and running coroutines. 3. Tasks: Wrappers around coroutines, representing asynchronous operations.  Let's start with a simple example to illustrate these concepts:  ```python import asyncio  async def greet(name):     print(f\"Hello, {name}!\")     await asyncio.sleep(1)     print(f\"Goodbye, {name}!\")  async def main():     await asyncio.gather(         greet(\"Alice\"),         greet(\"Bob\"),         greet(\"Charlie\")     )  asyncio.run(main()) ```  In this example, we define an asynchronous function `greet` using the `async def` syntax. The `await` keyword is used to pause the function's execution while waiting for an asynchronous operation (in this case, `asyncio.sleep`) to complete.  The `main` function uses `asyncio.gather` to run multiple coroutines concurrently. Finally, `asyncio.run` is called to start the event loop and execute our main coroutine.  When you run this script, you'll notice that all three greetings are printed almost simultaneously, followed by a one-second pause, and then all three farewells. This demonstrates how asyncio allows multiple operations to progress concurrently, even though we're using a single thread.  Working with Tasks  While coroutines are the building blocks of asynchronous code, tasks provide a higher-level abstraction for managing asynchronous operations. Let's explore how to create and work with tasks:  ```python import asyncio  async def fetch_data(url):     print(f\"Fetching data from {url}\")     await asyncio.sleep(2)  # Simulating network delay     print(f\"Data fetched from {url}\")     return f\"Data from {url}\"  async def main():     urls = [         \"https://api.example.com/users\",         \"https://api.example.com/products\",         \"https://api.example.com/orders\"     ]          tasks = [asyncio.create_task(fetch_data(url)) for url in urls]          results = await asyncio.gather(*tasks)          for result in results:         print(result)  asyncio.run(main()) ```  In this example, we create a task for each URL using `asyncio.create_task`. This allows us to start the coroutines immediately without waiting for each one to complete before moving to the next. We then use `asyncio.gather` to wait for all tasks to complete and collect their results.  This approach is particularly useful when dealing with multiple independent operations, such as fetching data from different API endpoints or processing multiple files concurrently.  Understanding the Event Loop  The event loop is the heart of asyncio. It's responsible for scheduling and running coroutines, handling I/O operations, and managing callbacks. While asyncio typically manages the event loop for you, understanding how it works can be beneficial for more advanced use cases.  Here's an example that demonstrates manual control of the event loop:  ```python import asyncio  async def countdown(name, n):     while n > 0:         print(f\"{name}: {n}\")         await asyncio.sleep(1)         n -= 1  async def main():     loop = asyncio.get_event_loop()     task1 = loop.create_task(countdown(\"Countdown 1\", 5))     task2 = loop.create_task(countdown(\"Countdown 2\", 3))          await task1     await task2  loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.close() ```  In this example, we manually create and manage the event loop. This level of control can be useful in more complex scenarios, such as integrating asyncio with other event-driven frameworks or implementing custom scheduling policies.  Handling Concurrent Network Operations  One of the most common use cases for asynchronous programming is handling network operations. Let's look at an example that simulates a simple chat server capable of handling multiple clients concurrently:  ```python import asyncio  class ChatServer:     def __init__(self):         self.clients = set()      async def handle_client(self, reader, writer):         self.clients.add(writer)         try:             while True:                 data = await reader.read(100)                 if not data:                     break                 message = data.decode()                 addr = writer.get_extra_info('peername')                 print(f\"Received {message} from {addr}\")                 for client in self.clients:                     if client != writer:                         client.write(f\"{addr}: {message}\".encode())                         await client.drain()         finally:             self.clients.remove(writer)             writer.close()      async def start_server(self):         server = await asyncio.start_server(             self.handle_client, '127.0.0.1', 8888)         addr = server.sockets[0].getsockname()         print(f'Serving on {addr}')         async with server:             await server.serve_forever()  asyncio.run(ChatServer().start_server()) ```  This chat server can handle multiple clients simultaneously, broadcasting messages to all connected clients. The `handle_client` coroutine is called for each new connection, allowing the server to manage multiple clients concurrently without blocking.  Implementing Asynchronous Context Managers  Asynchronous context managers are a powerful feature that allows you to manage resources asynchronously. They're particularly useful for operations that require setup and teardown, such as database connections or file I/O.  Here's an example of an asynchronous context manager for a simple database connection:  ```python import asyncio  class AsyncDatabaseConnection:     async def __aenter__(self):         print(\"Connecting to the database...\")         await asyncio.sleep(1)  # Simulating connection time         print(\"Connected!\")         return self      async def __aexit__(self, exc_type, exc_val, exc_tb):         print(\"Closing database connection...\")         await asyncio.sleep(0.5)  # Simulating disconnection time         print(\"Connection closed!\")      async def query(self, sql):         print(f\"Executing query: {sql}\")         await asyncio.sleep(0.5)  # Simulating query execution         return f\"Result of {sql}\"  async def main():     async with AsyncDatabaseConnection() as conn:         result1 = await conn.query(\"SELECT * FROM users\")         result2 = await conn.query(\"SELECT * FROM products\")         print(result1)         print(result2)  asyncio.run(main()) ```  This example demonstrates how to use the `async with` statement to manage the lifecycle of a database connection asynchronously. The `__aenter__` and `__aexit__` methods are called when entering and exiting the context, respectively.  Best Practices and Common Pitfalls  As you dive deeper into asynchronous programming with Python, keep these best practices and potential pitfalls in mind:  1. Avoid blocking operations: Ensure that all potentially blocking operations (e.g., file I/O, network calls) are performed asynchronously. Mixing synchronous and asynchronous code can lead to unexpected behavior.  2. Use asyncio.create_task() for concurrent operations: When you want to run multiple coroutines concurrently, create tasks using asyncio.create_task() rather than directly awaiting them.  3. Be cautious with shared state: When multiple coroutines access shared data, use appropriate synchronization primitives like asyncio.Lock to prevent race conditions.  4. Handle exceptions properly: Use try/except blocks within your coroutines to handle exceptions. Unhandled exceptions in coroutines can lead to unexpected behavior.  5. Avoid CPU-bound operations in coroutines: Asyncio is designed for I/O-bound operations. For CPU-intensive tasks, consider using multiprocessing or threading instead.  6. Use asyncio.gather() for concurrent tasks: When you need to wait for multiple tasks to complete, use asyncio.gather() instead of awaiting them individually.  7. Be mindful of callback hell: While asyncio helps avoid callback hell, it's still possible to create deeply nested coroutines. Use proper structuring and consider breaking complex operations into smaller, manageable coroutines.  Conclusion  We've covered the basics of asyncio, explored practical examples, and discussed best practices for writing asynchronous code. Armed with this knowledge, you're well-equipped to start incorporating asynchronous programming into your own projects.  Whether you're building web servers, network clients, or data processing pipelines, mastering asynchronous programming will give you the tools to take your Python projects to the next level. So go forth, embrace the async, and watch your applications soar to new heights of performance and scalability!  -Sethers"}
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "sethserver", "item": "https://www.sethserver.com/"}, {"@type": "ListItem", "position": 2, "name": "Programming", "item": "https://www.sethserver.com/programming/"}, {"@type": "ListItem", "position": 3, "name": "Python Asyncio Tutorial: Async/Await, Tasks & Concurrency Guide", "item": "https://www.sethserver.com/programming/asynchronous-programming-with-python-boosting-performance-and-scalability.html"}]}
```
