Python 地图没有返回任何东西
我有以下代码:Python 地图没有返回任何东西,python,multithreading,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 filepa
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
要获取上述映射中的return
语句,我需要做什么?t.join()
始终返回None
。这是因为线程目标的返回值被忽略
您必须通过其他方式收集结果,如:
从队列导入队列
结果=队列()
def upload_至_s3(文件路径,唯一_id):
#做点什么
打印s3#url#t.join()
始终返回None
。这是因为线程目标的返回值被忽略
您必须通过其他方式收集结果,如:
从队列导入队列
结果=队列()
def upload_至_s3(文件路径,唯一_id):
#做点什么
打印s3_url#谢谢,这很有意义。您能告诉我如何在上面的示例中使用队列
对象吗?谢谢,这很有意义。您能告诉我如何在上面的示例中使用队列
对象吗?
[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
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()