import threading


class SingletonMixin(object):
    __singleton_lock = threading.Lock()
    __singleton_instance = None

    @classmethod
    def instance(cls, *args, **kwargs):
        if not cls.__singleton_instance:
            with cls.__singleton_lock:
                if not cls.__singleton_instance:
                    cls.__singleton_instance = cls(*args, **kwargs)
        return cls.__singleton_instance


import threading


class SingletonMixin(object):
    __singleton_lock = None
    __singleton_instance = None

    @classmethod
    def instance(cls, *args, **kwargs):
        if not cls.__singleton_lock:
            cls.__singleton_lock = threading.Lock()

        if not cls.__singleton_instance:
            with cls.__singleton_lock:
                if not cls.__singleton_instance:
                    cls.__singleton_instance = cls(*args, **kwargs)
        return cls.__singleton_instance


Change singletons to instantiate locks per-class
Change singletons to instantiate locks per-class

Before it was creating one for any singleton object, resulting in
Bad Things (infinite lock) happening when one singleton object
instantiates another singleton object in its instantiation. (I hope
that made sense...)

  import threading
  
  
  class SingletonMixin(object):
-     __singleton_lock = threading.Lock()
+     __singleton_lock = None
      __singleton_instance = None
  
      @classmethod
      def instance(cls, *args, **kwargs):
+         if not cls.__singleton_lock:
+             cls.__singleton_lock = threading.Lock()
+ 
          if not cls.__singleton_instance:
              with cls.__singleton_lock:
                  if not cls.__singleton_instance:
                      cls.__singleton_instance = cls(*args, **kwargs)
          return cls.__singleton_instance
  
  
Change singletons to instantiate locks per-class
## Code Before:
import threading


class SingletonMixin(object):
    __singleton_lock = threading.Lock()
    __singleton_instance = None

    @classmethod
    def instance(cls, *args, **kwargs):
        if not cls.__singleton_instance:
            with cls.__singleton_lock:
                if not cls.__singleton_instance:
                    cls.__singleton_instance = cls(*args, **kwargs)
        return cls.__singleton_instance


## Instruction:
Change singletons to instantiate locks per-class
## Code After:
import threading


class SingletonMixin(object):
    __singleton_lock = None
    __singleton_instance = None

    @classmethod
    def instance(cls, *args, **kwargs):
        if not cls.__singleton_lock:
            cls.__singleton_lock = threading.Lock()

        if not cls.__singleton_instance:
            with cls.__singleton_lock:
                if not cls.__singleton_instance:
                    cls.__singleton_instance = cls(*args, **kwargs)
        return cls.__singleton_instance

