这样做安全吗?
@ndb.tasklet
def foo():
# Note that I do not yield right here
future = some_key.get_async()
# Perform some other code
if some_condition:
# I need the get_async result here
entity = yield future
# I also need the get_async result here.
# If some_condition was true, I would be yielding this future
# for the second time. Is this ok?
entity = yield future
请不要告诉我我可以只在函数顶部 yield
并在代码的其余部分使用 entity
。我的实际功能比这更复杂一些,并且我有一些可能需要使用 entity
的条件代码路径,并且我希望在最近的时刻yield
能够在后台获取实体时执行我的其他代码。
最佳答案
是的,很安全!
如果你检查ndb中的Future类,它会在完成时设置结果,并且多个yield或get_result将检查它是否完成并返回存储的结果。
google/appengine/ext/ndb/tasklets.py,SDK 1.7.4 中的第 324 行
关于google-app-engine - 多次生成 ndb.Future 是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14835696/