The asyncio
module in Python is used for writing concurrent code using asynchronous I/O. It provides an event loop, coroutines, tasks, and other tools for building asynchronous programs. This makes it ideal for handling tasks like web scraping, API calls, or any I/O-bound operation efficiently.
asyncio
is a library that helps you manage asynchronous programming. Unlike multithreading or multiprocessing, asyncio
uses a single thread but allows you to switch between tasks efficiently. This is especially useful when dealing with I/O-bound tasks like reading files, querying databases, or making network requests.
async def
that can be paused and resumed.This example demonstrates how to define and run a simple coroutine.
import asyncio async def say_hello(): print("Hello, asyncio!") await asyncio.sleep(1) print("Goodbye, asyncio!") asyncio.run(say_hello())
Hello, asyncio! Goodbye, asyncio!
You can run multiple coroutines concurrently 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
The asyncio.create_task
function schedules a coroutine to run concurrently as a 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, waiting for completion...") await task print("Task completed") asyncio.run(main())
Task created, waiting for completion... Number: 0 Number: 1 Number: 2 Number: 3 Number: 4 Task completed
Asynchronous context managers are often used for resources like database connections or file handling.
import asyncio class AsyncResource: async def __aenter__(self): print("Acquiring resource") await asyncio.sleep(1) return "Resource" async def __aexit__(self, exc_type, exc, tb): print("Releasing resource") await asyncio.sleep(1) async def main(): async with AsyncResource() as resource: print(f"Using {resource}") asyncio.run(main())
Acquiring resource Using Resource Releasing resource
Python's asyncio
module is a powerful tool for asynchronous programming. By understanding concepts like coroutines, tasks, and the event loop, you can build efficient and scalable programs for handling I/O-bound tasks. While it has a learning curve, the benefits of reduced resource usage and increased performance make it worth mastering.