2012-10-31 61 views
1

我正在使用Google App Engine和NDB。爲了簡潔,我刪除了很多代碼,但保留了基本問題。我發現了一個錯誤'list' object has no attribute 'get_result'爲什麼這個Python函數不會返回未來?

def get_future(keys): 
    future = ndb.get_multi_async(keys) 
    important_value = ... # get important value 
    return {"future" : future, "value" : important_value} 

dic = get_future(keys) 
future = dic['future'] 
# error `'list' object has no attribute 'get_result'` 
items = future.get_result() 

爲什麼我會得到一個列表回來時,我應該得到一個未來?

回答

8

get_multi_async實際上會返回一個future對象的列表,因此您需要在這些對象上調用.get_result()

Official definition

ndb.get_multi_async(鍵,** ctx_options)

異步取入由密鑰的傳遞序列標識的實體。

參數

keys 
-Sequence of keys 

**ctx_options 
-Context options 

返回未來的對象列表。如果未找到密鑰,則每個將來的結果都是一個Model實例或None。