异步不是加async就能提速,asyncio是协作式单线程调度器;await对CPU密集型操作无效,需用run_in_executor;event loop启动方式决定sync/async混用可行性;async with/for依赖特定协议,须用真正异步库。
异步不是加个 async 就能提速,Python 的 asyncio 是协作式单线程调度器,不是多线程或多进程的替代品。
await 只在遇到 awaitable 对象(如 async def 函数、asyncio.sleep()、aiohttp.get())时让出控制权;它不会自动把同步 CPU 操作变成非阻塞。
await time.sleep(1) 是错的——time.sleep() 是同步阻塞,根本不可等待,会直接报 TypeError: object of type 'NoneType' is not awaitable
loop.run_in_executor() 托管到线程池或进程池await hashlib.sha256(data).digest() —— 这仍是同步计算,await 在这里纯属语法错误主线程默认无运行中的 event loop;调用 asyncio.run() 会新建 loop 并关闭,但不能在已存在 loop 的环境(如 Jupyter、某些 Web 框架)里重复调用。
asyncio.run(main()) 第二次会报 RuntimeError: asyncio.run() cannot be called from a running event loop
asyncio.get_event_loop(),再用 loop.create_task() 或 asyncio.ensure_future()
run_until_complete(),否则可能冲突或导致死锁它们分别依赖对象实现 __aent 和
er__/__aexit____aiter__/__anext__,和普通 with / for 完全不兼容。
open('x.txt') 套 async with?报 AttributeError: __aenter__
aiofiles.open()、aiomysql.connect()、asyncpg.connect()
async for item in some_async_iterable 要求 some_async_iterable 返回的对象实现了异步迭代器协议,不是加了 async 的生成器就能用import asyncio import aiofilesasync 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。协程暂停点必须落在真正的异步原语上,否则调度器根本收不到信号。