1. The core principle of asyncio
1. Event Loop
-
effect: Event loop is
asyncio
The core scheduler is responsible for listening and managing all asynchronous tasks. -
Implementation principle:
- Maintain a task queue (
Task Queue
), execute coroutines according to priority. - Concurrency is handled through non-blocking I/O and callback mechanisms.
- use
epoll
(Linux)、kqueue
(macOS) orselect
(Cross-platform) to achieve efficient I/O multiplexing.
- Maintain a task queue (
2. Coroutine
-
definition:pass
async def
Defined functions that can be suspended and restored. -
Key mechanisms:
- use
await
Push the coroutine and hand over control to the event loop. - The coroutine status is saved in
generator
In the object, continue execution from the suspended point when recovering.
- use
3. Future and Task
- Future: Indicates the final result of an asynchronous operation (similar to Promise).
-
Task: Inherited from
Future
, used to wrap coroutines and schedule execution.
2. The difference between synchronous vs asynchronous
1. Comparison of execution modes
characteristic | Sync (Sync) | Asynchronous (Async) |
---|---|---|
Blocking behavior | Block the current thread | Non-blocking, suspend coroutine |
Concurrency method | Multithreaded/multiprocess | Single thread + event loop |
Applicable scenarios | CPU intensive tasks | I/O intensive tasks |
Resource overhead | High (thread/process switching) | Low (coroutine switch) |
2. Code comparison example
Synchronous blocking code
import time def sync_task(): (1) # Blocking threads print("Sync task done") start = () sync_task() sync_task() print(f"Total time consumption: {() - start:.2f}s") # The output is about 2.0s
Asynchronous non-blocking code
import asyncio async def async_task(): await (1) # Non-blocking hang print("Async task done") async def main(): start = () await (async_task(), async_task()) print(f"Total time consumption: {() - start:.2f}s") # The output is about 1.0s (main())
3. Detailed explanation of asyncio working mechanism
1. Task scheduling process
- Create an event loop and start.
- Wrapping coroutines as
Task
Objects join the queue. - The event loop polls the task status and executes the ready task.
- meet
await
hangs the current task,Switch to other tasks。 - Recover tasks via callback when I/O is completed.
2. Coroutine life cycle
Created → Pending → Running → Suspended (at await) → Resumed → Completed/Failed
4. Application fields and complete code examples
1. High-performance network crawler
import aiohttp import asyncio async def fetch(url): async with () as session: async with (url) as response: return await () async def main(): urls = [ "", "", "" ] tasks = [fetch(url) for url in urls] results = await (*tasks) for url, content in zip(urls, results): print(f"{url} Content length: {len(content)}") (main())
2. Real-time Web Services (FastAPI Integration)
from fastapi import FastAPI from import HTMLResponse import asyncio app = FastAPI() async def background_task(): while True: await (5) print("Background tasks are running...") @app.on_event("startup") async def startup_event(): asyncio.create_task(background_task()) @("/", response_class=HTMLResponse) async def read_root(): await (1) # Simulate asynchronous database query return "<h1>Hello Async World!</h1>"
3. Real-time data processing (WebSocket chat room)
import websockets import asyncio connected = set() async def chat_server(websocket): (websocket) try: async for message in websocket: await ( *[(f"User said: {message}") for client in connected] ) finally: (websocket) async def main(): async with (chat_server, "localhost", 8765): await () # Permanently run (main())
V. Advanced features and best practices
1. Coroutine synchronization mechanism
- Lock (Lock): Prevent competition for shared resources.
lock = () async def safe_update(): async with lock: # Modify shared resources
2. Error handling
async def risky_task(): try: await async_operation() except Exception as e: print(f"Error capture: {e}") async def main(): task = asyncio.create_task(risky_task()) await task
3. Performance tuning
- Limit the concurrency amount (if used
):
sem = (10) async def limited_task(): async with sem: await heavy_operation()
6. Summary of applicable scenarios
Scene Type | Case | Reasons for technical selection |
---|---|---|
High concurrency I/O | API aggregation services, crawlers | Reduce thread overhead and improve throughput |
Real-time communication | Chat room, online games | Low latency, long connection support |
Microservice architecture | FastAPI/gRPC server | Native asynchronous support, efficient request processing |
Timing tasks | Background data cleaning, heartbeat detection | Lightweight scheduling to avoid blocking the main thread |
7. Summary
asyncio
Through event loops and coroutine mechanisms, Python provides efficient asynchronous programming capabilities, suitable for I/O-intensive scenarios. Understanding its underlying principles (such as task scheduling, coroutine life cycle) is the key to optimizing performance. Selecting synchronous or asynchronous models in combination with specific scenarios can significantly improve program efficiency.
The above is the detailed content of the in-depth analysis of the Python asyncio library (including complete code and comments). For more information about the analysis of the Python asyncio library, please pay attention to my other related articles!