@@ -0,0 +1,725 @@
+## https://gist.github.com/werediver/4396488%0Aimport threading%0A%0Aclass Singleton(object):%0A    __singleton_lock = threading.Lock()%0A    __singleton_instance = None%0A%0A    @classmethod%0A    def instance(cls):%0A        if not cls.__singleton_instance :%0A            with cls.__singleton_lock:%0A                if not cls.__singleton_instance:%0A                    cls.__singleton_instance = cls()%0A%0A        return cls.__singleton_instance%0A%0Aif __name__ == '__main__':%0A%0A    a,a2 = Singleton.instance(), Singleton.instance()%0A    b,b2 = Singleton.instance(), Singleton.instance()%0A    assert a is a2%0A    assert b is b2%0A    #assert a is not b  # Gives assertion error%0A%0A    print('a:  %25s%5Cna2: %25s' %25 (a, a2))%0A    print('b:  %25s%5Cnb2: %25s' %25 (b, b2))%0A

Create singleton.py