title: Overhead in creating a thread vs process on Linux
body: <p>I am trying to answer the question of how much overhead there is in creating a thread vs a process in python. I modified code from a similar question which basically runs a function with two threads and then runs the same function with two processes and reports the time.</p>
  
  <pre><code>import time, sys
  NUM_RANGE = 100000000
  
  from multiprocessing  import Process
  import threading
  
  def timefunc(f):
      t = time.time()
      f()
      return time.time() - t
  
  def multiprocess():
      class MultiProcess(Process):
          def __init__(self):
              Process.__init__(self)
  
          def run(self):
              # Alter string + test processing speed
              for i in xrange(NUM_RANGE):
                  a = 20 * 20
  
  
      for _ in xrange(300):
        MultiProcess().start()
  
  def multithreading():
      class MultiThread(threading.Thread):
          def __init__(self):
              threading.Thread.__init__(self)
  
          def run(self):
              # Alter string + test processing speed
              for i in xrange(NUM_RANGE):
                  a = 20 * 20
  
      for _ in xrange(300):
        MultiThread().start()
  
  print "process run time" + str(timefunc(multiprocess))
  print "thread run time" + str(timefunc(multithreading))
  </code></pre>
  
  <p>Then I got 7.9s for multiprocessing and 7.9s for multithreading</p>
  
  <p>The main question I'm trying to answer is if it is appropriate to use multithreading or multiprocessing for thousands of network requests on Linux specifically. Seems like according to this code they are the same in terms of startup time but perhaps processes are much heavier in memory usage?</p>