异步不是加async就能提速,asyncio是协作式单线程调度器;await对CPU密集型操作无效,需用run_in_executor;event loop启动方式决定sync/async混用可行性;async with/for依赖特定协议,须用真正异步库。
异步不是加个
就能提速,Python 的
是协作式单线程调度器,不是多线程或多进程的替代品。
为什么 await 一个 CPU 密集型函数毫无意义
只在遇到
对象(如
函数、
、
)时让出控制权;它不会自动把同步 CPU 操作变成非阻塞。
写
是错的——
是同步阻塞,根本不可等待,会直接报
想在异步上下文中跑 CPU 密集任务,必须用
托管到线程池或进程池
常见误用:
—— 这仍是同步计算,
在这里纯属语法错误
event loop 启动方式决定你能不能混用 sync/async
主线程默认无运行中的 event loop;调用
会新建 loop 并关闭,但不能在已存在 loop 的环境(如 Jupyter、某些 Web 框架)里重复调用。
Jupyter 中执行
第二次会报
正确做法:检查是否已有 loop:
,再用
或
Flask/FastAPI 等框架已管理 loop,不应手动
,否则可能冲突或导致死锁
async with / async for 不是语法糖,是协议强制要求
它们分别依赖对象实现
和
,和普通
/
完全不兼容。
Python 3.14.3
微软官方的 Python 扩展,是 VS Code 安装量最高的扩展(209M+)。集成 IntelliSense(通过 Pylance)、调试(通过 Python Debugger)、代码检查、格式化、重构和单元测试等功能。支持 Jupyter Notebook、虚拟环境管理和多 Python 版本切换。
下载
立即学习
“
Python免费学习笔记(深入)
”;
用普通文件对象
套
?报
要用异步 I/O,得选真正支持的库:
、
、
要求
返回的对象实现了异步迭代器协议,不是加了
的生成器就能用
真正卡住异步系统的,往往不是
写错了,而是用了“看起来像异步”的同步库,或者在不该切换上下文的地方强行
。协程暂停点必须落在真正的异步原语上,否则调度器根本收不到信号。
asyncasyncioawaitawaitableasync defasyncio.sleep()aiohttp.get()await time.sleep(1)time.sleep()TypeError: object of type 'NoneType' is not awaitableloop.run_in_executor()await hashlib.sha256(data).digest()awaitasyncio.run()asyncio.run(main())RuntimeError: asyncio.run() cannot be called from a running event loopasyncio.get_event_loop()loop.create_task()asyncio.ensure_future()run_until_complete()__aenter__/__aexit____aiter__/__anext__withforopen('x.txt')async withAttributeError: __aenter__aiofiles.open()aiomysql.connect()asyncpg.connect()async for item in some_async_iterablesome_async_iterableasyncimport 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 系统调用
awaitawait