2013-02-12 72 views
2

这样做是否安全?多次产生ndb.Future是否安全?

@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在最后时刻能够执行我的其他代码,而实体正在背景。

回答

2

是的,它很安全!

如果您在ndb中检查Future类,它将在完成时设置结果,并且多个yield或get_result将检查它是否完成并返回存储的结果。

google/appengine/ext/ndb/tasklets.py,SDK 1.7.4中的第324行

+0

如果App Engine在将来更改其实现会怎么样?如果记录在案,我会感到更有信心。我的猜测是,这实际上不会改变;我的观点是,目前的实施不是保证,也不应该依赖。 – allyourcode 2013-04-09 22:50:48

+0

根据文档,你可以产生未来。屈服之后,未来仍然是未来吗? – tesdal 2013-04-16 19:33:10

相关问题