Python ThreadPoolExecutor 没有正确执行
Python ThreadPoolExecutor not executing proper
我正在使用 concurrent.futures 库来执行多线程 for 循环。每次都需要使用所有 5 个参数执行 for 循环。现在我已经到了我的 do_something_parallel
-函数只打印 "test1" 的地步。
现在的问题是在 do_something_parallel
-函数内部它不识别项目。因为当我打印错误时它说 AttributeError: <unknown>.Name
。在 for 循环中,我还尝试打印 item.Name
并且它有效。
from concurrent.futures import ThreadPoolExecutor
do_something_parallel(x, par2, par3, par4, par5):
print("test1")
print(str(x.Value))
print("test2")
main():
for i in range(0,38):
with ThreadPoolExecutor(max_workers=4) as executor:
futures = set()
for x in range(0,5):
print(str(item.Name)
f = executor.submit(do_something_parallel, x, par2, par3, par4, par5)
futures.add(f)
你必须把ThreadPoolExecutor放到迭代器外面,然后
模式将是这样的:
from concurrent import futures
from concurrent.futures import ThreadPoolExecutor
def do_something(*args, **kwargs):
""" Stub function to use with futures - your processing logic """
print("Do something in parallel")
return "result processed"
def main():
# The important part - concurrent futures
# - set number of workers as the number of jobs to process
# The number of workers you want to run in parallel
workers_range = 3
with ThreadPoolExecutor(len(your_range)) as executor:
# Use list jobs for concurrent futures
# Use list scraped_results for results
jobs = []
results_done = []
# Here you identify how many parallel tasks you want
# and what value you'll send to them
values = ["value1", "value2", "value3"] # as per workers_range
for value in values:
# Pass some keyword arguments if needed - per job
kw = {"some_param": value}
# Here we iterate 'number of dates' times, could be different
# We're adding scrape_func, could be different function per call
jobs.append(executor.submit(do_something, **kw))
# Once parallell processing is complete, iterate over results
for job in futures.as_completed(jobs):
# Read result from future
result_done = job.result()
# Append to the list of results
results_done.append(result_done)
# Iterate over results scraped and do whatever is needed
for result in results_done:
print("Do something with me {}".format(result))
只需遵循该模式即可使其正常工作。
我正在使用 concurrent.futures 库来执行多线程 for 循环。每次都需要使用所有 5 个参数执行 for 循环。现在我已经到了我的 do_something_parallel
-函数只打印 "test1" 的地步。
现在的问题是在 do_something_parallel
-函数内部它不识别项目。因为当我打印错误时它说 AttributeError: <unknown>.Name
。在 for 循环中,我还尝试打印 item.Name
并且它有效。
from concurrent.futures import ThreadPoolExecutor
do_something_parallel(x, par2, par3, par4, par5):
print("test1")
print(str(x.Value))
print("test2")
main():
for i in range(0,38):
with ThreadPoolExecutor(max_workers=4) as executor:
futures = set()
for x in range(0,5):
print(str(item.Name)
f = executor.submit(do_something_parallel, x, par2, par3, par4, par5)
futures.add(f)
你必须把ThreadPoolExecutor放到迭代器外面,然后 模式将是这样的:
from concurrent import futures
from concurrent.futures import ThreadPoolExecutor
def do_something(*args, **kwargs):
""" Stub function to use with futures - your processing logic """
print("Do something in parallel")
return "result processed"
def main():
# The important part - concurrent futures
# - set number of workers as the number of jobs to process
# The number of workers you want to run in parallel
workers_range = 3
with ThreadPoolExecutor(len(your_range)) as executor:
# Use list jobs for concurrent futures
# Use list scraped_results for results
jobs = []
results_done = []
# Here you identify how many parallel tasks you want
# and what value you'll send to them
values = ["value1", "value2", "value3"] # as per workers_range
for value in values:
# Pass some keyword arguments if needed - per job
kw = {"some_param": value}
# Here we iterate 'number of dates' times, could be different
# We're adding scrape_func, could be different function per call
jobs.append(executor.submit(do_something, **kw))
# Once parallell processing is complete, iterate over results
for job in futures.as_completed(jobs):
# Read result from future
result_done = job.result()
# Append to the list of results
results_done.append(result_done)
# Iterate over results scraped and do whatever is needed
for result in results_done:
print("Do something with me {}".format(result))
只需遵循该模式即可使其正常工作。