Asynchronous functions and coroutines in Python allow you to write non-blocking code that can perform multiple tasks concurrently. This is particularly useful for I/O-bound operations such as file reading, database access, or network communication. Python's asyncio
module provides the tools to define and manage asynchronous tasks.
An asynchronous function in Python is defined using the async def
keyword. These functions are also known as coroutines. Unlike regular functions, coroutines can pause their execution using the await
keyword and resume later, allowing other tasks to run during the pause.
Here is a basic example of an asynchronous function:
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
Inside CoroutinesThe await
keyword is used to pause the execution of a coroutine until the awaited task is completed. This is commonly used with other asynchronous calls.
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!
You can execute multiple coroutines at the same time using asyncio.gather
.
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
The asyncio.create_task
function allows you to schedule a coroutine to run as a task. This is useful for managing multiple coroutines independently.
import asyncio async def print_numbers(): for i in range(3): 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 Task completed
Async generators are a special kind of coroutine that can yield values asynchronously. They are defined using async def
and the yield
keyword.
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
Asynchronous functions and coroutines in Python offer an efficient way to handle multiple tasks concurrently. By leveraging the asyncio
module and its tools like await
, asyncio.gather
, and asyncio.create_task
, you can write scalable and responsive programs for various use cases.