Python异步编程asyncio深入理解 Python异步编程asyncio深入理解一、核心概念asyncio 基于事件循环调度协程- 协程coroutineasync def 定义的异步函数- 任务Task协程的并发调度包装器- Future异步操作的最终结果- 事件循环Event Loop调度和执行异步任务二、协程基础import asyncioasync def fetch(url, delay):await asyncio.sleep(delay)return f{url} 的数据async def main():result await fetch(api/users, 2)print(result)asyncio.run(main())await 暂停当前协程将控制权交还事件循环。三、并发执行# asyncio.gatherasync def main():results await asyncio.gather(fetch(api/users, 2),fetch(api/posts, 1),fetch(api/comments, 3),)# asyncio.create_taskasync def main():task1 asyncio.create_task(fetch(api/users, 2))task2 asyncio.create_task(fetch(api/posts, 1))result1 await task1result2 await task2# TaskGroup (Python 3.11)async with asyncio.TaskGroup() as tg:t1 tg.create_task(fetch(api/users, 2))t2 tg.create_task(fetch(api/posts, 1))四、异步迭代器class AsyncCounter:def __aiter__(self):return selfasync def __anext__(self):if self.current self.end:raise StopAsyncIterationawait asyncio.sleep(0.5)self.current 1return self.current - 1async for num in AsyncCounter(0, 5):print(num)五、超时与取消async def main():try:result await asyncio.wait_for(slow_op(), timeout3.0)except asyncio.TimeoutError:print(超时)task.cancel() # 取消任务六、异步同步原语- asyncio.Lock- asyncio.Semaphore限制并发数- asyncio.Queue生产者-消费者semaphore asyncio.Semaphore(5)async def limited_task(url):async with semaphore:return await fetch(url, 1)七、实际应用import aiohttpasync def fetch_all(urls):async with aiohttp.ClientSession() as session:tasks [session.get(url) for url in urls]return await asyncio.gather(*tasks)with ThreadPoolExecutor() as pool:result await loop.run_in_executor(pool, blocking_func)总结asyncio 通过单线程事件循环实现高并发IO避免了多线程的锁竞争。await 的暂停/恢复机制是理解的关键。