基础知识¶
The basics
AnyIO 需要 Python 3.8 或更高版本才能运行。建议在开发或使用 AnyIO 时设置 virtualenv 。
AnyIO requires Python 3.8 or later to run. It is recommended that you set up a virtualenv when developing or playing around with AnyIO.
安装¶
Installation
运行异步程序¶
Running async programs
最简单的 AnyIO 程序如下所示:
from anyio import run
async def main():
print('Hello, world!')
run(main)
这将在默认后端(asyncio)上运行上述程序。要在另一个支持的后端(如 Trio )上运行,可以使用 backend
参数,如下所示:
run(main, backend='trio')
但 AnyIO 代码并不要求通过 run()
运行。你也可以直接使用后端库的原生 run()
函数:
import sniffio
import trio
from anyio import sleep
async def main():
print('Hello')
await sleep(1)
print("I'm running on", sniffio.current_async_library())
trio.run(main)
在 4.0.0 版本发生变更: 在 asyncio
后端,对于 Python 3.11 之前的版本, anyio.run()
现在使用了回溯移植版的
asyncio.Runner
。
The simplest possible AnyIO program looks like this:
from anyio import run
async def main():
print('Hello, world!')
run(main)
This will run the program above on the default backend (asyncio). To run it on another
supported backend, say Trio, you can use the backend
argument, like so:
run(main, backend='trio')
But AnyIO code is not required to be run via run()
. You can just as well use the
native run()
function of the backend library:
import sniffio
import trio
from anyio import sleep
async def main():
print('Hello')
await sleep(1)
print("I'm running on", sniffio.current_async_library())
trio.run(main)
在 4.0.0 版本发生变更: On the asyncio
backend, anyio.run()
now uses a back-ported version of
asyncio.Runner
on Pythons older than 3.11.
后端特定选项¶
Backend specific options
Asyncio:
选项见
asyncio.Runner
的文档use_uvloop
(bool
, 默认为 False):如果可用,使用更快的 uvloop 事件循环实现(这是传递loop_factory=uvloop.new_event_loop
的简写,如果loop_factory
传递了非None
的值, 则该选项会被忽略)
Trio:选项请参考 官方文档
在 3.2.0 版本发生变更: use_uvloop
的默认值已更改为 False
。
在 4.0.0 版本发生变更: policy
选项已被 loop_factory
替代。
Asyncio:
options covered in the documentation of
asyncio.Runner
use_uvloop
(bool
, default=False): Use the faster uvloop event loop implementation, if available (this is a shorthand for passingloop_factory=uvloop.new_event_loop
, and is ignored ifloop_factory
is passed a value other thanNone
)
Trio: options covered in the official documentation
在 3.2.0 版本发生变更: The default value of use_uvloop
was changed to False
.
在 4.0.0 版本发生变更: The policy
option was replaced with loop_factory
.
使用原生异步库¶
Using native async libraries
AnyIO 允许你将为 AnyIO 编写的代码与为你选择的异步框架编写的代码混合使用。不过,有一些规则需要记住:
你只能使用与你运行的后端相对应的“原生”库,因此,例如,不能将为 Trio 编写的库与为 asyncio 编写的库一起使用。
在 Trio 以外的后端上,由这些“原生”库生成的任务不受 AnyIO 强制执行的取消规则的约束。
在 AnyIO 之外生成的线程不能使用
from_thread.run()
来调用异步代码。
AnyIO lets you mix and match code written for AnyIO and code written for the asynchronous framework of your choice. There are a few rules to keep in mind however:
You can only use "native" libraries for the backend you're running, so you cannot, for example, use a library written for Trio together with a library written for asyncio.
Tasks spawned by these "native" libraries on backends other than Trio are not subject to the cancellation rules enforced by AnyIO
Threads spawned outside of AnyIO cannot use
from_thread.run()
to call asynchronous code