This is my simple code where I want to run printRange() in parallel:
def printRange(lrange):
print ("First is " + str(lrange[0]) + " and last is " + str(lrange[1]))
def runInParallel():
ranges = [[0, 10], [10, 20], [20, 30]]
// Call printRange in parallel with each sublist of ranges given as argument
My question is different from this SO question as here, the each process is hardcoded, started and finally joined. I want to run printRange() in parallel with say 100 other printRange() worker functions. Hardcoding each time is not feasible. How could this be done?
Using multiprocessing
from multiprocessing import Pool
def print_range(lrange):
print('First is {} and last is {}'.format(lrange[0], lrange[1]))
def run_in_parallel():
ranges = [[0, 10], [10, 20], [20, 30]]
pool = Pool(processes=len(ranges))
pool.map(print_range, ranges)
if __name__ == '__main__':
run_in_parallel()
Output:
First is 0 and last is 10
First is 10 and last is 20
First is 20 and last is 30
Something like this?
import threading
def printRange(lrange):
print ("First is " + str(lrange[0]) + " and last is " + str(lrange[1]))
def runInParallel():
ranges = [[0, 10], [10, 20], [20, 30]]
for i in ranges:
t = threading.Thread(target=printRange, args = [i])
t.start()
runInParallel()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With