2014-09-22 141 views
0

我有以下代码:地图不返回任何东西

def upload_to_s3(filepath, unique_id): 
    # do something 
    print s3_url # <-- Confirming that this `s3_url` variable is not None 
    return s3_url 


threads = [] 
for num, list_of_paths in enumerate(chunked_paths_as_list): 
    for filepath in list_of_paths: 
     t = threading.Thread(target=upload_to_s3, args=(filepath, self.unique_id)) 
     t.start() 
     threads.append(t) 
results = map(lambda t: t.join(), threads) 
print results 

不幸的是,这是每一个项目返回None

[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] 
>>>>> TIME: 13.9884989262 

什么我需要做的就是在该return声明以上map

回答

6

t.join()总是返回None。这是因为线程目标的返回值被忽略。

你必须通过一些其他手段来收集你的结果,就像一个Queue object

from Queue import Queue 

results = Queue() 

def upload_to_s3(filepath, unique_id): 
    # do something 
    print s3_url # <-- Confirming that this `s3_url` variable is not None 
    results.put(s3_url) 


threads = [] 
for num, list_of_paths in enumerate(chunked_paths_as_list): 
    for filepath in list_of_paths: 
     t = threading.Thread(target=upload_to_s3, args=(filepath, self.unique_id)) 
     t.start() 
     threads.append(t) 
for t in threads: 
    t.join() 

while not results.empty(): 
    print results.get() 

或者,使用multiprocessing.dummy module得到multiprocessing.Pool行为,但是在多线程,它可以做你想做的;从异步函数调用收集返回值。

+0

谢谢,这是有道理的。你能告诉我如何在上面的例子中使用'Queue'对象吗? – David542 2014-09-22 20:08:38

+0

另请参阅http://stackoverflow.com/a/6894023/416467另一种方法。 – kindall 2014-09-22 21:15:17