使用线程返回传递给函数的参数
Get the parameters passed into function used a thread back
我希望能够访问传递给函数的参数,该函数已用于线程处理,以便将函数的结果与传递的参数相匹配。在 [=20 中使用 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]])
我希望能够访问传递给函数的参数,该函数已用于线程处理,以便将函数的结果与传递的参数相匹配。在 [=20 中使用 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]])