2014-09-22 91 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