17370845950

Python异步系统学习路线第539讲_核心原理与实战案例详解【技巧】
异步不是加async就能提速,asyncio是协作式单线程调度器;await对CPU密集型操作无效,需用run_in_executor;event loop启动方式决定sync/async混用可行性;async with/for依赖特定协议,须用真正异步库。

异步不是加个 async 就能提速,Python 的 asyncio 是协作式单线程调度器,不是多线程或多进程的替代品。

为什么 await 一个 CPU 密集型函数毫无意义

await 只在遇到 awaitable 对象(如 async def 函数、asyncio.sleep()aiohttp.get())时让出控制权;它不会自动把同步 CPU 操作变成非阻塞。

  • await time.sleep(1) 是错的——time.sleep() 是同步阻塞,根本不可等待,会直接报 TypeError: object of type 'NoneType' is not awaitable
  • 想在异步上下文中跑 CPU 密集任务,必须用 loop.run_in_executor() 托管到线程池或进程池
  • 常见误用:await hashlib.sha256(data).digest() —— 这仍是同步计算,await 在这里纯属语法错误

event loop 启动方式决定你能不能混用 sync/async

主线程默认无运行中的 event loop;调用 asyncio.run() 会新建 loop 并关闭,但不能在已存在 loop 的环境(如 Jupyter、某些 Web 框架)里重复调用。

  • Jupyter 中执行 asyncio.run(main()) 第二次会报 RuntimeError: asyncio.run() cannot be called from a running event loop
  • 正确做法:检查是否已有 loop:asyncio.get_event_loop(),再用 loop.create_task()asyncio.ensure_future()
  • Flask/FastAPI 等框架已管理 loop,不应手动 run_until_complete(),否则可能冲突或导致死锁

async with / async for 不是语法糖,是协议强制要求

它们分别依赖对象实现 __aenter__/__aexit____aiter__/__anext__,和普通 with / for 完全不兼容。

  • 用普通文件对象 open('x.txt')async with?报 AttributeError: __aenter__
  • 要用异步 I/O,得选真正支持的库:aiofiles.open()aiomysql.connect()asyncpg.connect()
  • async for item in some_async_iterable 要求 some_async_iterable 返回的对象实现了异步迭代器协议,不是加了 async 的生成器就能用
import asyncio
import aiofiles

async def read_large_file(): async with aiofiles.open('log.txt', mode='r') as f: async for line in f: if 'ERROR' in line: print(line.strip())

注意:aiofiles.open() 返回的是 AsyncContextManager,

它的 aenter 内部才真正发起异步 open 系统调用

真正卡住异步系统的,往往不是 await 写错了,而是用了“看起来像异步”的同步库,或者在不该切换上下文的地方强行 await。协程暂停点必须落在真正的异步原语上,否则调度器根本收不到信号。