Python异步编程模式从同步到异步的演进引言在Python开发中异步编程模式是构建高性能应用的关键。作为一名从Rust转向Python的后端开发者我深刻体会到异步编程在处理高并发场景时的优势。本文将深入探讨Python中的异步编程模式及其最佳实践。异步编程核心概念同步 vs 异步特性同步编程异步编程执行方式阻塞等待非阻塞回调资源利用线程阻塞事件循环复用并发模型多线程/多进程协程适用场景CPU密集型IO密集型编程复杂度简单较高异步编程模式分类回调模式传统异步编程方式协程模式使用async/await生产者-消费者模式队列驱动管道模式数据流处理环境搭建与基础配置异步Hello Worldimport asyncio async def main(): print(Hello) await asyncio.sleep(1) print(World) asyncio.run(main())事件循环配置import asyncio async def main(): loop asyncio.get_event_loop() print(fLoop running: {loop.is_running()}) policy asyncio.get_event_loop_policy() print(fPolicy: {policy}) asyncio.run(main())异步编程模式实战模式一回调模式import asyncio def callback(future): print(fCallback: {future.result()}) async def compute(): await asyncio.sleep(1) return 42 async def main(): loop asyncio.get_event_loop() future loop.create_task(compute()) future.add_done_callback(callback) await future asyncio.run(main())模式二协程模式import asyncio async def fetch_data(url): print(fFetching {url}) await asyncio.sleep(1) return fData from {url} async def main(): results await asyncio.gather( fetch_data(https://example.com), fetch_data(https://google.com), fetch_data(https://github.com) ) for result in results: print(result) asyncio.run(main())模式三生产者-消费者模式import asyncio import random async def producer(queue): for i in range(10): await asyncio.sleep(random.random()) item fItem {i} await queue.put(item) print(fProduced: {item}) async def consumer(queue, name): while True: item await queue.get() await asyncio.sleep(random.random()) print(fConsumer {name} consumed: {item}) queue.task_done() async def main(): queue asyncio.Queue() producer_task asyncio.create_task(producer(queue)) consumer_tasks [ asyncio.create_task(consumer(queue, A)), asyncio.create_task(consumer(queue, B)) ] await producer_task await queue.join() for task in consumer_tasks: task.cancel() asyncio.run(main())模式四管道模式import asyncio async def stage1(input_data): await asyncio.sleep(0.5) return input_data * 2 async def stage2(input_data): await asyncio.sleep(0.5) return input_data 10 async def stage3(input_data): await asyncio.sleep(0.5) return fResult: {input_data} async def pipeline(data): result await stage1(data) result await stage2(result) result await stage3(result) return result async def main(): results await asyncio.gather( pipeline(1), pipeline(2), pipeline(3) ) for result in results: print(result) asyncio.run(main())实际业务场景场景一API网关import asyncio import aiohttp async def handle_request(request): service_tasks [ fetch_user_service(request), fetch_product_service(request), fetch_order_service(request) ] results await asyncio.gather(*service_tasks) return { user: results[0], product: results[1], order: results[2] }场景二数据处理流水线import asyncio async def read_file(file_path): with open(file_path, r) as f: for line in f: yield line.strip() await asyncio.sleep(0) async def process_lines(lines): async for line in lines: processed line.upper() yield processed async def write_file(lines, output_path): with open(output_path, w) as f: async for line in lines: f.write(line \n) await asyncio.sleep(0) async def main(): lines read_file(input.txt) processed process_lines(lines) await write_file(processed, output.txt) asyncio.run(main())性能优化使用异步上下文管理器import asyncio class AsyncResource: async def __aenter__(self): await self.connect() return self async def __aexit__(self, exc_type, exc_val, exc_tb): await self.disconnect() async def connect(self): await asyncio.sleep(0.1) print(Connected) async def disconnect(self): await asyncio.sleep(0.1) print(Disconnected) async def main(): async with AsyncResource() as resource: print(Using resource) asyncio.run(main())使用异步迭代器import asyncio class AsyncDataStream: def __init__(self, count): self.count count self.index 0 def __aiter__(self): return self async def __anext__(self): if self.index self.count: raise StopAsyncIteration await asyncio.sleep(0.1) self.index 1 return self.index async def main(): async for item in AsyncDataStream(5): print(item) asyncio.run(main())总结异步编程模式为Python开发者提供了强大的并发处理能力。通过选择合适的异步模式可以显著提高应用的性能和响应性。从Rust开发者的角度来看Python的异步编程生态更加成熟和易用。在实际项目中建议根据业务场景选择合适的异步模式并注意代码的可读性和可维护性。