title: Python multiprocessing with pool and context manager
body: <p>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.</p>
  <p><strong>My code</strong></p>
  <pre><code>import concurrent.futures
  import numpy as np
  
  def do_it(n):
      for i in range(1,11):
          print(&quot;do_it instance&quot; + &quot; &quot; + str(n) + &quot;, iteration &quot; + str(i) + &quot; starting&quot;)
          try:
              rnd
          except NameError:
              print(&quot;do_it instance&quot; + &quot; &quot; + str(n) + &quot;, iteration &quot; + str(i) + &quot;: Variable is not defined&quot;)
          else:
              print(&quot;do_it instance&quot; + &quot; &quot; + str(n) + &quot;, iteration &quot; + str(i)+ &quot;: Variable is already defined as &quot; + str(random))
          rnd = np.random.randint(1,1000)
          print(&quot;do_it instance&quot; + &quot; &quot; + str(n) + &quot;, iteration &quot; + str(i) + &quot;: rnd set to &quot; + str(rnd))
          return rnd
  def main():
  
      with concurrent.futures.ProcessPoolExecutor() as executor:
          results = executor.map(do_it, range(1,11))
      print(results)
  </code></pre>
  <p>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?</p>
  <p><strong>Log</strong></p>
  <pre><code>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
  </code></pre>