Python中文
首页
教程
问答
标签
搜索
登录
注册
异步def函数的“屈服于”选项
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>在一个几乎100%异步的类中,我需要以同步(顺序)的方式执行一些代码(当前是一个协同程序)。 所以我创建了这个函数:</p> <pre><code>@asyncio.coroutine def execute_sequential(self, doo): for key in doo: yield from self.fetch_one_fin_instr_hist_data(doo[key]) </code></pre> <p>我用以下命令执行它:</p> <pre><code>tasks = [self.execute_sequential(doo)] await asyncio.gather(*tasks) </code></pre> <p>fetch\u one\u fin\u instr\u hist\u data</em>必须是一个协程,因为它是由异步函数调用的。 其签名如下:</p> <pre><code>async def fetch_one_fin_instr_hist_data(self, obj): </code></pre> <p>一切正常。 不幸的是<code>@asyncio.coroutine</code>已被弃用,因此我应该用<code>async def</code>关键字替换它。 <code>async def</code>不支持<code>yield from</code></p> <p>我尝试了以下方法:</p> <pre><code>async def execute_sequential(self, doo): for key in doo: for obj in self.fetch_one_fin_instr_hist_data(doo[key]): yield obj </code></pre> <p>但是在排队的时候</p> <pre><code>for obj in self.fetch_one_fin_instr_hist_data(doo[key]): </code></pre> <p>我得到一个错误:</p> <p><em>预期的类型为“collections.Iterable”,改为“Coroutine[Any,Any,None]”</em></p> <p>有没有办法将协同程序转换为iterable? 或者更好的是,在这种情况下,</em>的<em>产量的替代方案是什么</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>正如dirn在评论中提到的,<code>yield</code>被<code>await</code>取代:</p> <pre><code>async def execute_sequential(self, doo): for key in doo: for obj in self.fetch_one_fin_instr_hist_data(doo[key]): await obj </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Kerasterflow预训练模型中的纯训练偏差
4 回答
KerasTF Conv2D模型运行时无响应型号.fi
6 回答
Kerastuner Randomsearch:TypeError:(“关键字参数未理解:”,“激活”)
7 回答
Kerastuner ValueError:形状(320,)和(1,)不兼容
6 回答
Kerastuner:“ValueError:不是法律参数”问题,当我使用LSTM网络时,但密集层工作正常
2 回答
KerasTuner:是否可以在目标/度量函数中使用测试/验证集?
5 回答
KerasTuner自定义目标函数
2 回答
kerastuner调整层数会创建与报告的层数不同的层数
1 回答
KerasTuner运行时错误:构建模型的失败尝试太多
10 回答
kerasv1.2.2与kerasv2+的奇怪行为(精确度上的巨大差异)
1 回答
kerasvis中visualize_-cam/visualize_显著性的热图输出形状
9 回答
Kerasvis和tfkerasvis的激活最大化不适用于MobileNetV2模型
2 回答
Kerasvis对于显著性图表,我们应该使用softmax还是线性激活
4 回答
Kerasvis给出以下错误:AttributeError:多个入站节点
2 回答
kerasyolov3模型中预期输入和目标的格式和形状
8 回答
Keras一个GPU可以同时训练两个不相关的模型吗?
1 回答
Keras一类CNN两个输入,每一步一个
7 回答
keras三维张量上的Softmax层
8 回答
Keras三维目标预测
4 回答
keras上的flatten与python中的Image的区别
8 回答