title: Python sharing class instance among threads
body: <p>I have a class, that loads all resources into memory that are needed for my application (mostly images).</p>
  
  <p>Then several threads need to access these resources through this class.
  I don't want every instance to reload all resources, so I thought I use the Singleton Pattern.
  I did it like this:</p>
  
  <pre><code>class DataContainer(object):
      _instance = None
      _lock = threading.Lock()
      _initialised = True
  
      def __new__(cls, *args, **kwargs):
          with cls._lock:
              if not cls._instance:
                  cls._initialised = False
                  cls._instance = object.__new__(cls, *args, **kwargs)
          return cls._instance
  
      def __init__(self, map_name = None):
  
          # instance has already been created
          if self._initialised:
              return
  
          self._initialised = True
  
          # load images
  </code></pre>
  
  <p>This works fine, as long as I am not using multiple threads. But with multiple Threads every thread has a different instance. So using 4 threads, they each create a new instance.
  I want all threads to use the same instance of this class, 
  so the resources are only loaded into memory once.</p>
  
  <p>I also tried to do this in the same module where the class is defined, but outside the class definition:</p>
  
  <pre><code>def getDataContainer():
      global dataContainer
      return dataContainer
  
  dataContainer = DataContainer()
  </code></pre>
  
  <p>but every thread still has its own instance.</p>
  
  <p>I am new to python, if this is the wrong approach plz let me know, 
  I appreciate any help</p>