Skip to main content
 首页 » 编程设计

python之为什么这个 Python 函数不返回 future

2025年05月04日12zdz8207

我将 Google App Engine 与 NDB 结合使用。为了简洁起见,我删除了很多代码,但保留了基本问题。我遇到错误 'list' object has no attribute 'get_result'

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() 

为什么我应该得到一个 future 却得到一个列表?

请您参考如下方法:

get_multi_async 实际上返回 future 对象的列表,因此您需要对这些对象调用 .get_result()。

Official definition :

ndb.get_multi_async(keys, **ctx_options)

Asynchronously fetches entities identified by the passed sequence of keys.

Arguments

keys 
 -Sequence of keys 
 
**ctx_options  
 -Context options  

Returns a list of Future objects. Each future's result is a Model instance or None if the key wasn't found.