在Python的同步编程模型中,代码按照严格的顺序执行,当遇到I/O操作(如网络请求、文件读写、数据库查询)时,整个程序会陷入阻塞状态。这种阻塞会导致CPU资源的巨大浪费,特别是在处理高并发场景时,程序的吞吐量会急剧下降。
import time
def sync_task(n):
print(f"任务{n}开始")
time.sleep(1) # 模拟I/O阻塞
print(f"任务{n}完成")
start = time.time()
for i in range(3):
sync_task(i)
print(f"总耗时: {time.time()-start:.2f}秒")
执行结果:
任务0开始
任务0完成
任务1开始
任务1完成
任务2开始
任务2完成
总耗时: 3.00秒
异步编程通过协程(Coroutine)实现了非阻塞式的并发执行,能够在单个线程内处理多个I/O密集型任务。当遇到I/O等待时,程序会挂起当前任务,转而执行其他可运行的任务,从而显著提升程序的执行效率。
协程是一种用户态的轻量级线程,具有以下核心特性:
事件循环是异步编程的核心引擎,负责:
import asyncio
async def hello_coroutine():
print("Hello")
await asyncio.sleep(1)
print("Coroutine!")
async def main():
await hello_coroutine()
asyncio.run(main())
代码解析:
同步版本:
import time
def sync_task(n):
print(f"Sync task {n} start")
time.sleep(1)
print(f"Sync task {n} end")
start = time.time()
for i in range(3):
sync_task(i)
print(f"Sync total: {time.time()-start:.2f}s")
异步版本:
import asyncio
async def async_task(n):
print(f"Async task {n} start")
await asyncio.sleep(1)
print(f"Async task {n} end")
async def main():
tasks = [async_task(i) for i in range(3)]
await asyncio.gather(*tasks)
start = asyncio.get_event_loop().time()
asyncio.run(main())
print(f"Async total: {asyncio.get_event_loop().time()-start:.2f}s")
执行结果对比:
同步版本耗时约3秒
异步版本耗时约1秒
import asyncio
import random
async def producer(queue, name):
for i in range(3):
item = f"{name}-{i}"
await asyncio.sleep(random.random())
await queue.put(item)
print(f"生产 {item}")
async def consumer(queue):
while True:
item = await queue.get()
await asyncio.sleep(random.random())
print(f"消费 {item}")
queue.task_done()
async def main():
queue = asyncio.Queue(maxsize=2)
producers = [
asyncio.create_task(producer(queue, "P1")),
asyncio.create_task(producer(queue, "P2"))
]
consumers = [asyncio.create_task(consumer(queue)) for _ in range(2)]
await asyncio.gather(*producers)
await queue.join()
for c in consumers:
c.cancel()
asyncio.run(main())
import asyncio
from aiothrottle import Throttler
async def worker(n, throttler):
async with throttler:
print(f"Worker {n} start")
await asyncio.sleep(1)
print(f"Worker {n} done")
async def main():
# 限制每秒最多3个请求
throttler = Throttler(rate_limit=3, period=1)
tasks = [worker(i, throttler) for i in range(10)]
await asyncio.gather(*tasks)
asyncio.run(main())
import aiohttp
import asyncio
from bs4 import BeautifulSoup
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def parse(url):
async with aiohttp.ClientSession() as session:
html = await fetch(session, url)
soup = BeautifulSoup(html, 'lxml')
title = soup.find('title').text
print(f"{url} 标题: {title}")
async def main():
urls = [
'https://www.python.org',
'https://www.baidu.com',
'https://www.qq.com'
]
tasks = [parse(url) for url in urls]
await asyncio.gather(*tasks)
asyncio.run(main())
import asyncio
from async_profiler import profile
@profile
async def target_coroutine():
await asyncio.sleep(1)
# 业务代码
async def main():
await target_coroutine()
asyncio.run(main())
import asyncio
async def debug_demo():
await asyncio.sleep(0)
1/0 # 人为制造错误
async def main():
try:
await debug_demo()
except Exception as e:
print(f"捕获异常: {e}")
# 启用调试模式
asyncio.run(main(), debug=True)
类别 | 推荐库 | 同步库替代方案 |
---|---|---|
HTTP客户端 | aiohttp | requests |
数据库驱动 | asyncpg/aiomysql | psycopg2/pymysql |
Web框架 | FastAPI/Sanic | Flask/Django |
任务队列 | arq | Celery |
RPC | grpclib | grpcio |
特性 | Python | Go | JavaScript |
---|---|---|---|
并发模型 | 协程+事件循环 | Goroutine | Promise/Async |
调度方式 | 协作式 | 抢占式 | 协作式 |
内存消耗 | 约5KB/协程 | 2KB/Goroutine | 可变 |
开发复杂度 | 中等 | 低 | 中等 |
通过本文的系统学习,读者应该已经掌握:
建议下一步:
异步编程作为现代高并发系统的核心技术,正在深刻改变软件开发的面貌。掌握Python协程不仅能够提升程序性能,更能帮助开发者构建响应迅速、资源高效利用的现代应用系统。