title: Why is my print statement not reached after parallel processing?
body: <p>I've written a function that runs a stochastic simulation of a series of chemical reactions, heres a brief summary of the function:</p>
  <pre><code>def stochastic_simulation(*args): # arguments are a series of arrays
      t = SimulationTimer()
      t.start()
      update molecule numbers for each species in model
      update current time of the system 
      t.stop()
      print(f&quot;Accumulated time: {t.get_accumulated_time():0.10f} seconds&quot;)
  return popul_num_all, tao_all, t.get_accumulated_time() # popul_num_all is an array of changing molecule numbers over time, tao_all is the evolution of time throughout the simulation. 
  </code></pre>
  <p>The <code>SimulationTimer</code> is a custom made timer:</p>
  <pre><code>class TimeError(Exception):
  &quot;&quot;&quot;A custom exception used to report errors in use of Timer Class&quot;&quot;&quot; 
  pass
  
  class SimulationTimer:  
      def __init__(self):
          self._simulation_start_time = None
          self._simulation_stop_time = None 
          self.accumulated_elapsed_time = 0.0
  
      def start(self):
          &quot;&quot;&quot;start a new timer&quot;&quot;&quot;
          if self._simulation_start_time is not None:    # attribute
              raise TimeError(f&quot;Timer is running.\n Use .stop() to stop it&quot;)
  
          self._simulation_start_time = time.perf_counter()  
      def stop(self):
          &quot;&quot;&quot;stop the time and report the elsaped time&quot;&quot;&quot;
          if self._simulation_start_time is None:
              raise TimeError(f&quot;Timer is not running.\n Use .start() to start it.&quot;)
  
          self._simulation_stop_time = time.perf_counter()
          elapsed_simulation_time = self._simulation_stop_time - 
          self._simulation_start_time  
          self.accumulated_elapsed_time += elapsed_simulation_time
  
          self._simulation_start_time = None
          print(f&quot;Elapsed time: {elapsed_simulation_time:0.10f} seconds&quot;)
  
      def get_accumulated_time(self):
          &quot;&quot;&quot; Return the elapsed time for later use&quot;&quot;&quot;
          return self.accumulated_elapsed_time
  </code></pre>
  <p>I'm trying to run <code>stochastic_simulation</code> in parallel using the <code>pool</code> method of the <code>multiprocessing</code> library. <code>t.get_accumulated_time()</code> is meant to be the time it takes to run all my simulations in parallel.</p>
  <pre><code>if __name__ == '__main__':
      with Pool() as p:
          pool_results = p.map(stochastic_simulation, [start_state, LHS, stoch_rate, state_change_array])
  
          total_time = 0.0
          for tuple_results in pool_results:
              total_time += tuple_results[2] 
      print(f&quot;Total time:\n{total_time}&quot;)
  </code></pre>
  <p>The above code is how I run the parallel simulations, and there are two issues with it. Firstly, It never actually reaches the print statement, <code>f&quot;Total time:\n{total_time}&quot;</code> is never printed. Secondly, it never returns one combined measure of time taken to run all parallel simulations <code>t.get_accumulated_time()</code> instead it returns a separate time for each of the processes.</p>
  <p>I'm not sure if these issues are to do with <code>scoping</code> the <code>class object</code> or the <code>multiprocessing</code>.</p>
  <p>EDIT:</p>
  <p>My inputs into the <code>stochastic_simulation</code>are all arrays.
  That describe the following system:</p>
  <pre><code>1S + 0T + 0U --&gt; 0S + 0T + 0U
  2S + 0T + 0U --&gt; 0S + 1T + 0U 
  0S + 1T + 0U --&gt; 2S + 0T + 0U
  0S + 1T + 0U --&gt; 0S + 0T + 1U   
  </code></pre>
  <ol>
  <li><code>initial_state</code> this is the number of each molecule species at time zero. in my model there are 4 species so it has len = 3</li>
  <li><code>LHS</code> is a 2D array of ratios between the reactants of different reactions.</li>
  <li><code>stoch_rate</code> is the rate of each reaction an array of length 4</li>
  <li><code>state_change_array</code> is the net change in molecules after a reaction fires.</li>
  </ol>
  <p>Any suggestions?</p>