Python Async and Await

Python 12 min min read Updated: Mar 09, 2026 Advanced
Python Async and Await
Advanced Topic 7 of 10

Async & Await in Python

Asynchronous programming is a programming technique that allows tasks to run concurrently without blocking the execution of other tasks. In Python, the async and await keywords are used to write asynchronous code that can handle multiple operations efficiently.

This approach is especially useful for applications that perform many I/O operations such as network requests, database queries, file handling, or web scraping.

What is Asynchronous Programming?

In synchronous programming, tasks run sequentially, meaning each operation must complete before the next one starts. In contrast, asynchronous programming allows tasks to run concurrently so that the program can perform other work while waiting for slow operations to finish.

This improves performance and responsiveness, particularly in applications that interact with external systems.

The async Keyword

The async keyword is used to define an asynchronous function. These functions are also known as coroutines.

python import asyncio async def greet(): print("Hello")

The function defined with async will not run immediately until it is awaited.

The await Keyword

The await keyword pauses the execution of the coroutine until the awaited task is completed.

python import asyncio async def greet(): print("Hello") await asyncio.sleep(2) print("World") asyncio.run(greet())

This program pauses for 2 seconds before printing "World".

Running Multiple Async Tasks

Python allows running multiple asynchronous tasks concurrently using asyncio.gather().

python import asyncio async def task1(): await asyncio.sleep(1) print("Task 1 completed") async def task2(): await asyncio.sleep(2) print("Task 2 completed") async def main(): await asyncio.gather(task1(), task2()) asyncio.run(main())

Both tasks run concurrently instead of sequentially.

Event Loop in Python

The event loop is the core component that manages asynchronous execution in Python. It schedules and runs asynchronous tasks.

The asyncio.run() function creates and runs the event loop automatically.

python asyncio.run(main())

Async Functions Returning Values

Async functions can also return values using the return statement.

python import asyncio async def calculate(): await asyncio.sleep(1) return 10 async def main(): result = await calculate() print(result) asyncio.run(main())

Real-World Example

Asynchronous programming is commonly used for performing multiple network requests efficiently.

python import asyncio async def download_file(name): print(f"Downloading {name}") await asyncio.sleep(2) print(f"{name} downloaded") async def main(): await asyncio.gather( download_file("file1"), download_file("file2"), download_file("file3") ) asyncio.run(main())

This example simulates downloading multiple files concurrently.

Async vs Multithreading vs Multiprocessing

Feature Async Programming Multithreading Multiprocessing
Best For I/O-bound tasks I/O-bound tasks CPU-bound tasks
Memory Single thread Shared memory Separate memory
Performance Efficient concurrency Limited by GIL True parallel execution

Best Practices for Async Programming

  • Use async for I/O-heavy tasks such as APIs and database calls.
  • Avoid blocking operations inside async functions.
  • Use asyncio.gather() to run multiple tasks concurrently.
  • Keep coroutines lightweight and efficient.

Conclusion

Async and await provide a powerful way to write concurrent programs in Python. By using asynchronous programming, developers can build scalable applications that efficiently handle multiple tasks such as network operations, data streaming, and background processing.

Understanding async programming is essential for modern Python applications, especially in web frameworks, APIs, and data processing systems.

In the next tutorial, we will explore Python Logging & Debugging and learn how to monitor and troubleshoot Python applications effectively.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators