Python异步编程实践 Python异步编程通过事件循环和协程实现高并发I/O操作。 asyncio基础 asyncio是Python内置的异步I/O库。 基本使用 创建任务 并发执行 aiohttp异步HTTP aiohttp是基于asyncio的异步HTTP框架。 基本请求 并发请求 限制并发 异步编程模式 生产者消费者 超时控制 最佳实践 使用asyncio.run()作为入口 避免在协程中使用阻塞操作 控制并发数量避免资源耗尽 使用asyncio.sleep而非time.sleep 合理使用异常处理 Python异步编程适合I/O密集型任务,能显著提升并发性能。
Python异步编程通过事件循环和协程实现高并发I/O操作。
asyncio是Python内置的异步I/O库。
import asyncio async def main(): print('Start') await asyncio.sleep(1) print('End') asyncio.run(main())
async def task(): await asyncio.sleep(1) return 'result' async def main(): t = asyncio.create_task(task()) result = await t print(result)
async def fetch(url): await asyncio.sleep(1) return f'data from {url}' async def main(): urls = ['url1', 'url2', 'url3'] results = await asyncio.gather(*[fetch(u) for u in urls]) print(results)
aiohttp是基于asyncio的异步HTTP框架。
import aiohttp async def fetch(url): async with aiohttp.ClientSession() as session: async with session.get(url) as response: return await response.text()
async def fetch_all(urls): async with aiohttp.ClientSession() as session: tasks = [fetch(session, url) for url in urls] return await asyncio.gather(*tasks)
async def fetch_limited(urls, limit=10): semaphore = asyncio.Semaphore(limit) async with aiohttp.ClientSession() as session: async def fetch_with_limit(url): async with semaphore: return await fetch(session, url) return await asyncio.gather(*[fetch_with_limit(u) for u in urls])
async def producer(queue): for i in range(5): await queue.put(f'item-{i}') async def consumer(queue): while True: item = await queue.get() if item is None: break print(item) async def main(): queue = asyncio.Queue() await asyncio.gather(producer(queue), consumer(queue))
async def with_timeout(): try: async with asyncio.timeout(3): await slow_operation() except asyncio.TimeoutError: print('timeout')
Python异步编程适合I/O密集型任务,能显著提升并发性能。