python - 过早关闭嵌套的异步生成器
问题描述
我正在寻找如何在不完全迭代的情况下关闭嵌套的异步生成器。例如:
import asyncio
async def gen1():
yield 1
yield 2
async def gen2():
async for nr in gen1():
yield nr
async def main():
stream = gen2()
print(await stream.__anext__()) # 1
await stream.aclose()
asyncio.run(main())
但是,这会导致RuntimeError
:
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-2' coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
解决方案
使用 Python 版本3.7.4
和3.8.0b4
. 它已被修复3.8.0rc1
:
推荐阅读
- position - 使用卡尔曼滤波器估计位置
- c# - .NET 5 异常引发内容流错误
- sql - 使用两个表获取项目的降序列表
- gremlin - Gremlin 计算与今天的日期差
- python - 在 init 中使用实例方法
- java - 创建聚合管道时,AccumulatorOperators Max 出现问题
- linux - 在 ODI 中从另一台机器导入场景 xml
- android - 菜单未在右侧对齐
- ruby-on-rails - 授权用户在不使用 Pundit 的情况下为每个控制器执行各种 CRUD 操作;Ruby on Rails
- post - 带有字符串格式的 protobuf post rpc 方法