现代Python通过asyncio模块实现的事件循环,本质是单线程下的调度器:
import asyncio
async def fetch_data(url):
reader, writer = await asyncio.open_connection(url, 80)
writer.write(b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
data = await reader.read(1000)
return data.decode()
关键突破点:
性能对比测试显示(模拟1000并发请求):
方案内存占用完成时间CPU利用率多线程1.2GB12.3s85%asyncio80MB8.7s92%多进程2.4GB15.1s70%
def handle_exception(loop, context):
logger.error(f"Unhandled exception: {context}")
loop = asyncio.get_event_loop()
loop.set_exception_handler(handle_exception)
async def worker(queue):
while True:
task = await queue.get()
await process_task(task)
queue.task_done()