asyncio.run()只能调用一次,因其内部创建并关闭事件循环;await后必须是真正的awaitable对象,如asyncio.sleep而非time.sleep;create_task()实现并发调度,而直接await则顺序执行。

asyncio 的核心原理和实战案例,真正卡住人的从来不是语法,而是事件循环怎么调度、协程对象怎么挂起、await 底层怎么触发状态切换。
asyncio.run() 为什么只能调用一次?
因为 asyncio.run() 内部会创建新事件循环、运行完就关闭。再调用就会报 RuntimeError: asyncio.run() cannot be called from a running event loop。
- 开发时别在 Jupyter 或已启动
asyncio的环境中反复执行asyncio.run(main()) - 想多次运行,改用
asyncio.get_event_loop().run_until_complete(main())(注意:Python 3.10+ 推荐用asyncio.new_event_loop()+set_event_loop()显式管理) -
asyncio.run()隐含了loop.close(),关闭后无法复用
await 后面必须是 awaitable,但不是所有“能 await 的东西”都安全
比如 await time.sleep(1) 看似合理,实则阻塞整个事件循环——time.sleep 是同步阻塞函数,根本不是 awaitable。
- 正确做法是用
await asyncio.sleep(1) - 第三方库如
requests没有异步支持,直接await requests.get(...)会报TypeError: object Response can't be used in 'await' expression - 数据库操作要用
aiomysql、asyncpg、motor这类原生 async 驱动,不能套壳await loop.run_in_executor(..., sqlite3.connect)(虽可行但掩盖问题)
asyncio.create_task() 和直接 await 的区别不只是“后台执行”
关键在于调度时机:await coro 是立即进入并阻塞等待完成;asyncio.create_task(coro) 把协程注册进事件循环,当前函数可继续往下走,下次循环 tick 才开始执行。
import asyncio
async def say(what, delay):
await asyncio.sleep(delay)
print(what)
async def main():
task1 = asyncio.create_task(say("hello", 2))
task2 = asyncio.create_task(say("world", 1))
print("tasks created")
await task1
await task2
asyncio.run(main())
输出顺序是:tasks created → world → hello。如果写成 await say("hello", 2),那 say("world", 1) 就得等 2 秒后才开始。
立即学习“Python免费学习笔记(深入)”;
async with 和 async for 不是语法糖,它们依赖 __aenter__/__aexit__ 和 __aiter__/__anext__
没实现这些方法的对象,哪怕加了 async 前缀,也不能用于 async with。常见坑:
-
aiofiles.open()可以,open()不行 -
aiomysql.Pool支持async with pool.acquire(),但自己写的普通上下文管理器加async不等于自动支持 -
async for line in file:要求file实现__aiter__,不是所有文件对象都满足(比如io.StringIO就不行)
async def,是理解「谁在什么时候让出控制权」「事件循环在哪一刻把哪个协程切回来」。调试时多打 print(f"at {inspect.currentframe().f_lineno}"),比看文档更快定位挂起点。










