ndb aysnc"yield next"消耗get_multi_async

Bri*_*unt 5 python google-app-engine app-engine-ndb

是否有内置或规范的方式来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)):
   ...
Run Code Online (Sandbox Code Playgroud)

消除未来消耗的一种方法是检查futures是否已完成.存在固有的竞争条件:如果任何期货同时或在任何情况下在remove通话之前完成,futures则可以完成多个元素.否则,我不知道确定消费未来的可靠方法.

人们会认为这是一种非常常见的模式,但我还没有看到它解决了.翻翻ndb/tasklets.py,似乎有一些异国情调(读:无证)可能性,例如ReducingFutureMultiFuture,但我从来没有使用过.也许答案在于tasklets本身.

Gui*_*sum 7

这很容易 - 只需使用一套:

futures = set(futures)
while futures:
    f = ndb.Future.wait_any(futures)
    futures.remove(f)
    yield f
Run Code Online (Sandbox Code Playgroud)