I am relatively new to Python and new to Python multiprocessing. I am trying to execute a function repeatedly (10 times in the example below) as separate processes. Within the function I have a loop of multiple iterations (again, 10 times in the example below). In this example, and for simplicity, the function just sets a random number. Judging by the logging I've added, I have at least two problems. Firstly, while I see iteration 1 of each process instance starting and setting the random number, iteration 2 of each process instance starts but appears to do nothing beyond that and iterations 3-10 of each process don't appear to start at all. Secondly, there appears to be cross-contamination between process instances, with multiple processes apparently setting the same random number.
My code
import concurrent.futures
import numpy as np
def do_it(n):
for i in range(1,11):
print("do_it instance" + " " + str(n) + ", iteration " + str(i) + " starting")
try:
rnd
except NameError:
print("do_it instance" + " " + str(n) + ", iteration " + str(i) + ": Variable is not defined")
else:
print("do_it instance" + " " + str(n) + ", iteration " + str(i)+ ": Variable is already defined as " + str(random))
rnd = np.random.randint(1,1000)
print("do_it instance" + " " + str(n) + ", iteration " + str(i) + ": rnd set to " + str(rnd))
return rnd
def main():
with concurrent.futures.ProcessPoolExecutor() as executor:
results = executor.map(do_it, range(1,11))
print(results)
My aim is to end up with a list of 100 random numbers, one from each of the 10 process instances and 10 iterations therein. There's undoubtedly a simple explanation and solution to this but searching online for answers has not helped me. Are there any Python experts who can help?
Log
do_it instance 1, iteration 1 starting
do_it instance 1, iteration 1: Variable is not defined
do_it instance 2, iteration 1 starting
do_it instance 2, iteration 1: Variable is not defined
do_it instance 3, iteration 1 starting
do_it instance 3, iteration 1: Variable is not defined
do_it instance 1, iteration 1: rnd set to 807
do_it instance 1, iteration 2 starting
do_it instance 2, iteration 1: rnd set to 807
do_it instance 2, iteration 2 starting
do_it instance 3, iteration 1: rnd set to 807
do_it instance 3, iteration 2 starting
do_it instance 4, iteration 1 starting
do_it instance 4, iteration 1: Variable is not defined
do_it instance 4, iteration 1: rnd set to 666
do_it instance 4, iteration 2 starting
do_it instance 5, iteration 1 starting
do_it instance 5, iteration 1: Variable is not defined
do_it instance 5, iteration 1: rnd set to 666
do_it instance 5, iteration 2 starting
do_it instance 6, iteration 1 starting
do_it instance 6, iteration 1: Variable is not defined
do_it instance 6, iteration 1: rnd set to 807
do_it instance 6, iteration 2 starting
do_it instance 7, iteration 1 starting
do_it instance 8, iteration 1 startingdo_it instance 7, iteration 1: Variable is not defined
do_it instance 8, iteration 1: Variable is not defined
do_it instance 8, iteration 1: rnd set to 666
do_it instance 8, iteration 2 startingdo_it instance 7, iteration 1: rnd set to 666
do_it instance 9, iteration 1 startingdo_it instance 7, iteration 2 starting
do_it instance 9, iteration 1: Variable is not defined
do_it instance 10, iteration 1 startingdo_it instance 9, iteration 1: rnd set to 947
do_it instance 9, iteration 2 startingdo_it instance 10, iteration 1: Variable is not defined
do_it instance 10, iteration 1: rnd set to 947
do_it instance 10, iteration 2 starting