Python introduced the async
and await
keywords to support asynchronous programming. These keywords help developers write non-blocking code that can perform multiple tasks concurrently, making it especially useful for I/O-bound operations such as network requests, file handling, and database access.
async
in Python?The async
keyword is used to define a coroutine, which is a special type of function that can pause execution and allow other tasks to run. Coroutines are the building blocks of asynchronous programming in Python.
await
in Python?The await
keyword is used to pause the execution of a coroutine until the awaited task is completed. It can only be used inside a coroutine defined with async def
.
async
and await
This example demonstrates how to define and run a simple coroutine using async
and await
.
import asyncio async def greet(): print("Hello, World!") await asyncio.sleep(1) print("Goodbye, World!") # Running the coroutine asyncio.run(greet())
Hello, World! Goodbye, World!
await
to Call Other CoroutinesYou can use await
to call other coroutines, allowing them to pause and resume execution.
import asyncio async def fetch_data(): print("Fetching data...") await asyncio.sleep(2) print("Data fetched!") async def process_data(): print("Processing data...") await asyncio.sleep(1) print("Data processed!") async def main(): await fetch_data() await process_data() asyncio.run(main())
Fetching data... Data fetched! Processing data... Data processed!
The asyncio.gather
function allows you to run multiple coroutines concurrently.
import asyncio async def task_one(): print("Task one starting") await asyncio.sleep(2) print("Task one completed") async def task_two(): print("Task two starting") await asyncio.sleep(1) print("Task two completed") async def main(): await asyncio.gather(task_one(), task_two()) asyncio.run(main())
Task one starting Task two starting Task two completed Task one completed
asyncio.create_task
You can create and manage tasks independently using asyncio.create_task
.
import asyncio async def print_numbers(): for i in range(5): print(f"Number: {i}") await asyncio.sleep(1) async def main(): task = asyncio.create_task(print_numbers()) print("Task created!") await task print("Task completed") asyncio.run(main())
Task created! Number: 0 Number: 1 Number: 2 Number: 3 Number: 4 Task completed
async
and await
with Asynchronous GeneratorsAsynchronous generators allow you to yield values asynchronously using async def
and yield
.
import asyncio async def async_generator(): for i in range(3): await asyncio.sleep(1) yield i async def main(): async for value in async_generator(): print(f"Generated: {value}") asyncio.run(main())
Generated: 0 Generated: 1 Generated: 2
async
and await
The async
and await
keywords in Python simplify asynchronous programming, making it easier to write efficient and non-blocking code. By mastering these concepts and the tools provided by the asyncio
module, developers can handle I/O-bound tasks more effectively.