2013-02-12 43 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

相關問題