我希望能够访问传入函数的参数,该函数已用于线程,以匹配函数的结果与传入的参数。在Python中使用ThreadPoolExecutor
.
data = [[url1, 1], [url2, 2], [url3, 3]]
lst = []
with concurrent.futures.ThreadPoolExecutor() as executor:
results = [executor.submit(check, var1, var2) for var1, var2 in data]
for f in concurrent.futures.as_completed(results):
result = f.result()
if result:
lst.append([result, ????])
所以我想要'????'作为列表'data'中与'result'相对应的元素
欢呼使用字典,键作为Future
对象,值作为传递的参数。
results = {executor.submit(check, var1, var2): (var1, var2) for var1, var2 in data}
当你想引用的时候,就用lst.append([result, results[f]])
data = [[url1, 1], [url2, 2], [url3, 3]]
lst = []
with concurrent.futures.ThreadPoolExecutor() as executor:
results = {executor.submit(check, var1, var2): (var1, var2) for var1, var2 in data}
for f in concurrent.futures.as_completed(results.keys()):
result = f.result()
if result:
lst.append([result, results[f]])