title: Python multiprocessing: Global name is not defined
body: <p>I am trying to use the Python multiprocessing lib but run into problems with globally defined variables. For some reason I get the error "NameError: global name ... is not defined" The original program is quite involved so I have reproduced my problem in the code below: </p>
  
  <pre><code>import multiprocessing
  import time
  import timeit
  
  def setGlobal():
  
      global globalVar
      globalVar = 2
  
  def mp_worker(arguments):
  
      # do some processing.... 
      data = arguments[0]    
      # need to use global data
      the_time = globalVar    
  
      time.sleep(int(the_time))
  
  def f(processes = 3):
  
      global pool   
      pool = multiprocessing.Pool(processes)
  
  def mp_handler(number):
  
      inputs = []
      inputs.append([1]) 
      inputs.append([2]) 
  
      print("Begin multiprocessing test...for test "+str(number))
      start_time = timeit.default_timer()    
      pool.map(mp_worker, inputs)
      print("Time taken to calculate trades: %4.4f " %(timeit.default_timer() - start_time))
  
  
  if __name__ == '__main__':
      #define global variable
      setGlobal()
      # initialise pool
      f(3)
  
      # Get error: NameError: global name 'globalVar' is not defined 
      mp_handler(1)
  </code></pre>
  
  <p>Now a potential solution I tried was: </p>
  
  <pre><code>import multiprocessing
  import time
  import timeit
  
  globalVar = 4
  
  def setGlobal(number):
  
      global globalVar
      globalVar = number
  
  def mp_worker(arguments):
  
      # do some processing.... 
      data = arguments[0]    
      # need to use global data
      print("GlobalVar in my_worker = " + str(globalVar))
      the_time = globalVar
      time.sleep(int(the_time))
  
  def f(processes = 3):
  
      global pool   
      pool = multiprocessing.Pool(processes)
  
  def mp_handler(number):
  
      inputs = []
      inputs.append([1]) 
      inputs.append([2]) 
      print("GlobalVar in my_handler = " + str(globalVar))
  
      print("Begin multiprocessing test...for test "+str(number))
      start_time = timeit.default_timer()    
      pool.map(mp_worker, inputs)
      print("Time taken to calculate trades: %4.4f " %(timeit.default_timer() - start_time))
  
  
  if __name__ == '__main__':
  
      # initialise pool
      f(3)
  
      mp_handler(1)
  
      # re-set global variable... has no effect   
      setGlobal(1)
      mp_handler(2)
  </code></pre>
  
  <p>However when I call setGlobal(1) it has no effect in mp_worker? The globally defined value is 4 when I need it to be 1. Why is this and can this problem be overcome? </p>