地图不归还任何东西

时间:2014-09-22 20:03:55

标签: python multithreading

我有以下代码:

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声明?

1 个答案:

答案 0 :(得分: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行为,但使用线程,这可以做你想要的;从异步函数调用中收集返回值。