适用于Python 3.5+的异步生成器和上下文管理器
项目描述
async_generator库
Python 3.6添加了异步生成器。 (什么是异步生成器? 查看我在PyCon 2016的5分钟闪电演讲演示。)Python 3.7添加了一些工具使它们可用,如contextlib.asynccontextmanager。
此库将所有这些功能都带回了Python 3.5。
例如,此代码只能在Python 3.6+中运行
async def load_json_lines(stream_reader):
async for line in stream_reader:
yield json.loads(line)
但此代码执行相同的操作,并在Python 3.5+上运行
from async_generator import async_generator, yield_
@async_generator
async def load_json_lines(stream_reader):
async for line in stream_reader:
await yield_(json.loads(line))
或者,在Python 3.7中,你可以写
from contextlib import asynccontextmanager
@asynccontextmanager
async def background_server():
async with trio.open_nursery() as nursery:
value = await nursery.start(my_server)
try:
yield value
finally:
# Kill the server when the scope exits
nursery.cancel_scope.cancel()
这与之前相同,但回退到3.5
from async_generator import async_generator, yield_, asynccontextmanager
@asynccontextmanager
@async_generator
async def background_server():
async with trio.open_nursery() as nursery:
value = await nursery.start(my_server)
try:
await yield_(value)
finally:
# Kill the server when the scope exits
nursery.cancel_scope.cancel()
(如果你在3.6,你可以使用@asynccontextmanager与原生生成器。)
让我们这样做
安装: python3 -m pip install -U async_generator(或在Windows上,可能是py -3 -m pip install -U async_generator)
许可: MIT或Apache 2,您选择
贡献指南:https://trio.readthedocs.io/zh/latest/contributing.html
行为准则:请所有贡献者在所有项目空间中遵守我们的行为准则。
为什么有些链接提到了“trio”?
Trio 是一个专注于可用性和正确性的 Python 异步并发库 – 我们希望它易于使用,并确保正确。作为这一使命的一部分,async_generator 库由 Trio 项目维护,因为 Trio 内部使用了 async_generator。
您可以使用 async_generator 与任何异步库配合使用。它与 asyncio、Twisted 或您喜欢的任何库配合得很好。(但我们认为 Trio 非常棒。)
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。
源分布
async_generator-1.10.tar.gz (29.9 kB 查看哈希)
构建分布
async_generator-1.10-py3-none-any.whl (18.9 kB 查看哈希)
关闭
async_generator-1.10.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6ebb3d106c12920aaae42ccb6f787ef5eefdcdd166ea3d628fa8476abe712144 |
|
MD5 | 078a29b4afb3d7f38c097a530f042a55 |
|
BLAKE2b-256 | ceb66fa6b3b598a03cba5e80f829e0dadbb49d7645f523d209b2fb7ea0bbb02a |
关闭
async_generator-1.10-py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 01c7bf666359b4967d2cda0000cc2e4af16a0ae098cbffcb8472fb9e8ad6585b |
|
MD5 | f42a694c403397d825208a4cf97379e6 |
|
BLAKE2b-256 | 715239d20e03abd0ac9159c162ec24b93fbcaa111e8400308f2465432495ca2b |