title: Using threading/multiprocessing in python to do multiple calculations at the same time
body: <p>I have a list of numbers. I want to perform some time-consuming operation on each number in the list and make a new list with all the results. Here's a simplified version of what I have:</p>
  
  <pre><code>def calcNum(n):#some arbitrary, time-consuming calculation on a number
    m = n
    for i in range(5000000):
      m += i%25
      if m &gt; n*n:
        m /= 2
    return m
  
  nums = [12,25,76,38,8,2,5]
  finList = []
  
  for i in nums:
    return_val = calcNum(i)
    finList.append(return_val)
  
  print(finList)
  </code></pre>
  
  <p>Now, I wanted to take advantage of the multiple cores in my CPU, and give each of them a task of processing one of the numbers, and since the "number calculation" function is self-contained from start to finish I figured this would be fairly simple to do and a perfect situation for multiprocessing/threading.</p>
  
  <p>My question is, which one should I use (multiprocessing or threading?), and what is the simplest way to do this?</p>
  
  <p>I did a test with various code I found in other questions to achieve this, and while it runs fine it doesn't seem to be doing any actual multithreading/processing and takes just as long as my first test:</p>
  
  <pre><code>from multiprocessing.pool import ThreadPool
  
  def calcNum(n):#some arbitrary, time-consuming calculation on a number
    m = n
    for i in range(5000000):
      m += i%25
      if m &gt; n*n:
        m /= 2
    return m
  
  pool = ThreadPool(processes=3)
  
  nums = [12,25,76,38,8,2,5]
  finList = []
  
  for i in nums:
    async_result = pool.apply_async(calcNum, (i,))
    return_val = async_result.get()
    finList.append(return_val)
  
  print(finList)
  </code></pre>