代码点读机 / Python / async

async Python · 关键字

定义一个协程函数,函数可以在等待 IO 时让出执行权,适合并发任务。

怎么用

async def 函数名():

小例子

async def 打招呼():
    return 'hi'
print(type(打招呼))

运行结果

<class 'function'>

提醒:async 函数需要用 await 或 asyncio.run 来驱动。

官方文档:https://docs.python.org/zh-cn/3/reference/compound_stmts.html#coroutine-function-definition

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条