gpt4 book ai didi

python - ndb 异步 "yield next"消费 get_multi 异步

转载 作者:太空狗 更新时间:2023-10-30 00:55:56 25 4
gpt4 key购买 nike

是否有内置或规范的方法来按完成顺序使用 ndb.get_multi_async 调用的第一个和所有后续结果?

我希望,这说明了,它遵循以下原则:

def yield_next(futures):
while futures:
yield ndb.Future.wait_any(futures)
# We have to remove the yielded future from the futures.
# How do we know which future was removed?
# ... futures.remove(???)

for model in yield_next(ndb.get_multi_async(keys)):
...

删除已消耗的 future 的一种方法是检查 futures 以查看它是否已完成。存在固有的竞争条件:如果任何 future 同时完成或在任何情况下在 remove 调用之前完成,则 futures 的多个元素可能完成。否则我不知道确定哪个 future 被消耗的可靠方法。

人们会认为这是一种很常见的模式,但我还没有看到它得到解决。查看 ndb/tasklets.py 似乎有一些奇特的(阅读:未记录的)可能性,如 ReducingFutureMultiFuture,但我从未使用过他们。也许答案在于 tasklet 本身。

最佳答案

这很简单——只需使用一组:

futures = set(futures)
while futures:
f = ndb.Future.wait_any(futures)
futures.remove(f)
yield f

关于python - ndb 异步 "yield next"消费 get_multi 异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22862913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com