title: State shared in multiprocessing when using singleton classes
body: <p>Here is a toy example of the problem I am having. I have a Singleton class which is used in a large python script. I want to run this script many times with multiple inputs:
  The singleton pattern is not necessary here, but in my more complicated real use there is a reason to use it</p>
  
  <pre><code>import time
  import multiprocessing
  
  
  class TestClass(object):
    instance = None
  
    @classmethod
    def get_instance(cls):
      if cls.instance is None:
        print 'creating instance'
        cls.instance = TestClass()
      return cls.instance
  
    def __init__(self):
      self.data = []
  
  
  def worker(num):
    tc = TestClass.get_instance()
    time.sleep(0.1)
    tc.data.append(num)
    return tc.data
  
  
  def main():
    pool = multiprocessing.Pool(processes=1)
    res = pool.map(worker, range(10))
  
    print res 
    print TestClass.get_instance().data
  
  
  main()
  </code></pre>
  
  <p>When I run the above code, it seems the state of the <code>TestClass.instance</code> is (semi-?)shared. The result is:</p>
  
  <pre><code>[[0, 1, 2], [0, 1, 2], [0, 1, 2], ..., [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
  </code></pre>
  
  <p>If I change the number of processes to 10 I get:</p>
  
  <pre><code>[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
  </code></pre>
  
  <p>This is the result I would like.</p>
  
  <p>(If I print <code>TestClass.get_instance().data</code> after calling <code>pool.map()</code> Then I get an empty list.)</p>
  
  <p>What is the explanation for this behavior?
  Is there a way to stop this state sharing and get the second output, but keep control of the number of processes in the pool?</p>
  
  <p>Edit:
  When I choose N instances to run the function with M different arguments.
  An instance is created N times. Ideally I want M instances created, one for each argument.</p>