Python asyncio 模块详解及使用示例
asyncio
是 Python 用于编写并发代码的库,使用 async/await
语法。它是 Python 异步编程的核心库,特别适合 I/O 密集型和高并发网络应用。
主要方法及功能
1. 事件循环管理
asyncio.run(coro, *, debug=False)
- 运行异步程序的主入口asyncio.get_event_loop()
- 获取当前事件循环asyncio.set_event_loop(loop)
- 设置当前事件循环asyncio.new_event_loop()
- 创建新的事件循环loop.run_until_complete(future)
- 运行直到 future 完成loop.run_forever()
- 一直运行事件循环loop.stop()
- 停止事件循环loop.close()
- 关闭事件循环
2. 协程与任务
asyncio.create_task(coro)
- 将协程包装为任务asyncio.gather(*aws, return_exceptions=False)
- 并发运行多个协程asyncio.wait(aws, *, timeout=None, return_when=ALL_COMPLETED)
- 等待多个协程完成asyncio.shield(aw)
- 保护协程不被取消asyncio.sleep(delay, result=None)
- 异步等待
3. Future 对象
asyncio.Future()
- 创建一个 Future 对象future.add_done_callback(callback)
- 添加完成回调future.result()
- 获取结果future.exception()
- 获取异常future.done()
- 检查是否完成future.cancel()
- 取消 future
4. 同步原语
asyncio.Lock()
- 异步锁asyncio.Event()
- 异步事件asyncio.Condition()
- 异步条件变量asyncio.Semaphore(value)
- 信号量asyncio.BoundedSemaphore(value)
- 有界信号量
5. 子进程与线程
asyncio.subprocess
- 异步子进程loop.run_in_executor(executor, func, *args)
- 在 executor 中运行函数
实际使用示例
示例1: 基本异步函数
import asyncio
async def say_hello():
print("Hello")
await asyncio.sleep(1)
print("World")
async def main():
await say_hello()
asyncio.run(main())
示例2: 并发执行多个任务
import asyncio
import time
async def task(name, delay):
print(f"Task {name} started")
await asyncio.sleep(delay)
print(f"Task {name} completed after {delay}s")
async def main():
start_time = time.time()
# 使用 gather 并发运行多个任务
await asyncio.gather(
task("A", 2),
task("B", 1),
task("C", 3)
)
print(f"Total time: {time.time() - start_time:.2f}s")
asyncio.run(main())
输出:
Task A started
Task B started
Task C started
Task B completed after 1s
Task A completed after 2s
Task C completed after 3s
Total time: 3.00s
示例3: 异步网络请求
import asyncio
import aiohttp # 需要安装: pip install aiohttp
async def fetch_url(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def main():
urls = [
'https://www.example.com',
'https://www.python.org',
'https://www.google.com'
]
tasks = [fetch_url(url) for url in urls]
results = await asyncio.gather(*tasks)
for url, content in zip(urls, results):
print(f"URL: {url}, Length: {len(content)}")
asyncio.run(main())
示例4: 使用锁同步
import asyncio
async def worker(lock, name):
print(f"Worker {name} waiting for lock")
async with lock:
print(f"Worker {name} acquired lock")
await asyncio.sleep(1)
print(f"Worker {name} released lock")
async def main():
lock = asyncio.Lock()
await asyncio.gather(
worker(lock, "A"),
worker(lock, "B"),
worker(lock, "C")
)
asyncio.run(main())
示例5: 异步生产者消费者模式
import asyncio
import random
async def producer(queue, name):
for i in range(3):
item = f"Item-{name}-{i}"
await asyncio.sleep(random.random())
await queue.put(item)
print(f"Producer {name} produced {item}")
await queue.put(None) # 发送结束信号
async def consumer(queue, name):
while True:
item = await queue.get()
if item is None:
queue.put(None) # 让其他消费者也能结束
break
print(f"Consumer {name} consumed {item}")
await asyncio.sleep(random.random())
async def main():
queue = asyncio.Queue()
producers = [producer(queue, f"P{i}") for i in range(2)]
consumers = [consumer(queue, f"C{i}") for i in range(3)]
await asyncio.gather(*producers, *consumers)
asyncio.run(main())
示例6: 异步超时处理
import asyncio
async def long_running_task():
try:
print("Task started")
await asyncio.sleep(10)
print("Task completed")
return "Success"
except asyncio.CancelledError:
print("Task cancelled")
raise
async def main():
try:
# 设置3秒超时
result = await asyncio.wait_for(long_running_task(), timeout=3)
print(f"Result: {result}")
except asyncio.TimeoutError:
print("Task timed out")
asyncio.run(main())
总结
asyncio
模块提供了完整的异步编程解决方案,包括:
- 事件循环管理
- 协程和任务管理
- 同步原语
- 子进程和线程集成
- 网络和子进程I/O支持
在实际应用中,asyncio
特别适合处理高并发的I/O密集型任务,如网络请求、数据库访问等场景。通过合理使用异步编程,可以显著提高程序的吞吐量和响应速度。