I have a list of numbers as described below (Elements):
Elements = [['1','5'], ['2','5'], ['3','5'], ['4','5'], ['5', '5'], ['6', '5'], ['7', '5'], ['8', '5'], ['9', '5'], ['10', '5']]
I want to call a function main(x1,y1)
using process where x1=records[0]
, y1=records[1]
for records in Elements
. I would like to call main()
with say 10 processes to work simultaneously on first 10 records from Elements
. Once it is finished, call next 10 records from Elements and do the same and repeat until all the records are processed by main(). I am new to python so it would be great if anyone could help me with this.
Here's my code-
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)
You can do something like this with 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']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.