我有一个如下所述的数字列表(元素):
Elements = [['1','5'], ['2','5'], ['3','5'], ['4','5'], ['5', '5'], ['6', '5'], ['7', '5'], ['8', '5'], ['9', '5'], ['10', '5']]
我想使用main(x1,y1)
,x1=records[0]
为y1=records[1]
中的记录调用函数Elements
。
我想将main()
称为10个进程同时处理来自Elements
的前10个记录。完成后,从Elements调用接下来的10条记录并执行相同的操作并重复,直到main()处理完所有记录。我是python的新手,所以如果有人能帮我解决这个问题会很棒。
这是我的代码 -
def main(x1,y1):
do something
do something
import multiprocessing as mp
output = mp.Queue()
processes = [mp.Process(target=main, args=(records[0], records[1], output)) for records in Elements]
# Run processes
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
# Get process results from the output queue
results = [output.get() for p in processes]
print(results)
答案 0 :(得分:0)
您可以使用Pool执行此类操作:
from multiprocessing import Pool
PROCESS_COUNT = 10
elements = [['1','5'], ['2','5'], ['3','5'], ['4','5'], ['5', '5'], ['6', '5'], ['7', '5'], ['8', '5'], ['9', '5'], ['10', '5']]
def main(element):
# let's say you want to have concatenations as the results
return element[0] + element[1]
pool = Pool(PROCESS_COUNT)
results = pool.map(main, elements)
pool.close()
pool.join()
# now results is a list of concatenations: ['15', '25', '35', '45', '55', '65', '75', '85', '95', '105']