title: Change Global Variable in Multiprocessing Python
body: <pre><code>import random
  import threading
  import multiprocessing as mp
  import time
  </code></pre>
  
  <p>This is the global variable I want every process to change</p>
  
  <pre><code>result = 0
  lock = mp.Lock()
  </code></pre>
  
  <p>Function I used to test the multiprocessing performance</p>
  
  <pre><code>def list_append(count, id, out_list):
  
      #lock.acquire()
      global result
      result = result+1
      #lock.release()
      for i in range(count):
          out_list.append(random.random())
  </code></pre>
  
  <p>Using multiprocessing I have a good performance but I am not able to     change the global variable.     </p>
  
  <pre><code>start = time.time()
  if __name__ == "__main__":
      size = 10000000   # Number of random numbers to add
      threads = 6   # Number of threads to create
      # Create a list of jobs and then iterate through
      # the number of threads appending each thread to
      # the job list
      #global result
      jobs = []
      for i in range(0, threads):
          out_list = list()
          process = mp.Process(target=list_append,args=(size, i, out_list))
          jobs.append(process)
      # Start the threads (i.e. calculate the random number lists)
      for j in jobs:
              j.start()
  
      # Ensure all of the threads have finished
      for j in jobs:
          j.join()
  
      print ("List processing complete.")
      end = time.time()
      print(end - start)
      print(result)
  </code></pre>
  
  <p>Using threading, I am able to change the global variable value but with poor performance. </p>
  
  <pre><code>start = time.time()
  if __name__ == "__main__":
      size = 10000000   # Number of random numbers to add
      threads = 6   # Number of threads to create
  
  # Create a list of jobs and then iterate through
  # the number of threads appending each thread to
  # the job list
      jobs = []
      for i in range(0, threads):
          out_list = list()
          thread = threading.Thread(target=list_append(size, i, out_list))
          jobs.append(thread)
  # Start the threads (i.e. calculate the random number lists)
      for j in jobs:
          j.start()
  
  # Ensure all of the threads have finished
      for j in jobs:
          j.join()
  
      print ("List processing complete.")
      end = time.time()
      print(end - start)
      print(result)
  </code></pre>
  
  <p>I would like to know how I can change the global variable value using multiprocessing.</p>