asyncio 参考

Basics

import asyncioImport asyncio
async def main(): passAsync function (coroutine)
await asyncio.sleep(1)Async sleep
asyncio.run(main())Run coroutine (entry point)
result = await coro()Await coroutine

Tasks & Concurrency

task = asyncio.create_task(coro())Schedule coroutine
await asyncio.gather(c1(), c2(), c3())Run concurrently
results = await asyncio.gather(*coros)Gather list of coroutines
done, pending = await asyncio.wait(tasks)Wait with more control
asyncio.Semaphore(10)Limit concurrency

Async Context & IO

async with aiofiles.open('f') as f:Async file IO
async for item in aiter:Async iteration
async with asyncio.timeout(5):Timeout (3.11+)