asyncio.run()只能调用一次,因其内部创建并关闭事件循环;await后必须是真正的awaitable对象,如asyncio.sleep而非time.sleep;create_task()实现并发调度,而直接await则顺序执行。这标题看着像教程,实际不是入门向内容——
asyncio 的核心原理和实战案例,真正卡住人的从来不是语法,而是事件循环怎么调度、协程对象怎么挂起、await 底层怎么触发状态切换。
因为 asyncio.run() 内部会创建新事件循环、运行完就关闭。再调用就会报 RuntimeError: asyncio.run() cannot be called from a running event loop。
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 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)(虽可行但掩盖问题)
执行”关键在于调度时机: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 秒后才开始。
没实现这些方法的对象,哪怕加了 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}"),比看文档更快定位挂起点。