Python ndb aysnc“;“下一个收益率”;要使用get_multi_async
对于Python ndb aysnc“;“下一个收益率”;要使用get_multi_async,python,google-app-engine,app-engine-ndb,Python,Google App Engine,App Engine Ndb,对于ndb.get\u multi\u 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
ndb.get\u multi\u 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)):
...
删除已消耗的未来的一种方法是检查期货
,查看是否已完成。
存在一个固有的竞争条件:如果任何期货同时完成,或者在remove
调用之前的任何情况下,可以执行futures
的多个元素。除此之外,我还不知道有什么可靠的方法来确定哪个未来被消耗掉了
人们可能会认为这是一个相当普遍的模式,但我还没有看到它得到解决。通过查看ndb/tasklets.py
似乎有一些奇异的(阅读:未记录的)可能性,如减少未来
或多未来
,但我从未使用过它们。也许答案在于微线程本身。很简单——只需使用一组:
futures = set(futures)
while futures:
f = ndb.Future.wait_any(futures)
futures.remove(f)
yield f