how to create singleton class with arguments in python
Question:
I’m looking for the right way to create a singleton class that accepts arguments in the first creation.
My research lead me to 3 different ways:
Metaclass
class Singleton(type):
instance = None
def __call__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls.instance
class ASingleton(metaclass=Singleton):
pass
__new__
class Singleton(object):
instance = None
def __new__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super().__new__(cls, *args, **kwargs)
return cls.instance
Decorator
def Singleton(myClass):
instances={}
def getInstance(*args, **kwargs):
if myClass not in instances:
instances[myClass] = myClass(*args, **kwargs)
return instances[myClass]
return getInstance
@Singleton
class SingletonTest(object):
pass
All of them work fine, but when it comes to initiation (like using __init__ in normal class) I can’t figure out the right way to implement it.
The only solution I can think about is using the metaclass method in this way:
class Singleton(type):
instance = None
def __call__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls.instance
class ASingleton(metaclass=Singleton):
def __init__(self,j):
self.j=j
I don’t know if this is the correct way to create singleton that accepts arguments.
Answers:
I’ve found out that the best way to implement Singleton is by using meta classes:
class Singleton (type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
# Python 2
class MyClass():
__metaclass__= Singleton
# Python 3
class MyClass(metaclass=Singleton):
pass
In addition to @AndriyIvaneyko’s answer, here is a thread-safe metaclass singleton implementation:
# Based on tornado.ioloop.IOLoop.instance() approach.
# See https://github.com/facebook/tornado
# Whole idea for this metaclass is taken from: https://stackoverflow.com/a/6798042/2402281
import threading
class ThreadSafeSingleton(type):
_instances = {}
_singleton_lock = threading.Lock()
def __call__(cls, *args, **kwargs):
# double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
if cls not in cls._instances:
with cls._singleton_lock:
if cls not in cls._instances:
cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class YourImplementation(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
pass # your implementation goes here
Hope you find it useful!
I think the solution proposed by @tahesse can raise a dead lock. If the __init__
method contains another singleton than the lock won’t be released.
Foe example :
class ThreadSafeSingleton(type):
_instances = {}
_singleton_lock = threading.Lock()
def __call__(cls, *args, **kwargs):
# double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
if cls not in cls._instances:
with cls._singleton_lock:
if cls not in cls._instances:
cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class YourImplementation1(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
pass # your implementation goes here
def simple_method(self):
return "this is a test"
class YourImplementation2(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
self.your_implementation1 = YourImplementation1()
def simple_method(self):
print(self.your_implementation1.simple_method())
So I changed that solution a bit
class ThreadSafeSingleton(type):
_instances = {}
_singleton_locks: Dict[Any, threading.Lock] = {}
def __call__(cls, *args, **kwargs):
# double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
if cls not in cls._instances:
if cls not in cls._singleton_locks:
cls._singleton_locks[cls] = threading.Lock()
with cls._singleton_locks[cls]:
if cls not in cls._instances:
cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class YourImplementation1(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
pass # your implementation goes here
def simple_method(self):
return "this is a test"
class YourImplementation2(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
self.your_implementation1 =YourImplementation1()
def simple_method(self):
print(self.your_implementation1.simple_method())
I’m looking for the right way to create a singleton class that accepts arguments in the first creation.
My research lead me to 3 different ways:
Metaclass
class Singleton(type):
instance = None
def __call__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls.instance
class ASingleton(metaclass=Singleton):
pass
__new__
class Singleton(object):
instance = None
def __new__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super().__new__(cls, *args, **kwargs)
return cls.instance
Decorator
def Singleton(myClass):
instances={}
def getInstance(*args, **kwargs):
if myClass not in instances:
instances[myClass] = myClass(*args, **kwargs)
return instances[myClass]
return getInstance
@Singleton
class SingletonTest(object):
pass
All of them work fine, but when it comes to initiation (like using __init__ in normal class) I can’t figure out the right way to implement it.
The only solution I can think about is using the metaclass method in this way:
class Singleton(type):
instance = None
def __call__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls.instance
class ASingleton(metaclass=Singleton):
def __init__(self,j):
self.j=j
I don’t know if this is the correct way to create singleton that accepts arguments.
I’ve found out that the best way to implement Singleton is by using meta classes:
class Singleton (type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
# Python 2
class MyClass():
__metaclass__= Singleton
# Python 3
class MyClass(metaclass=Singleton):
pass
In addition to @AndriyIvaneyko’s answer, here is a thread-safe metaclass singleton implementation:
# Based on tornado.ioloop.IOLoop.instance() approach.
# See https://github.com/facebook/tornado
# Whole idea for this metaclass is taken from: https://stackoverflow.com/a/6798042/2402281
import threading
class ThreadSafeSingleton(type):
_instances = {}
_singleton_lock = threading.Lock()
def __call__(cls, *args, **kwargs):
# double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
if cls not in cls._instances:
with cls._singleton_lock:
if cls not in cls._instances:
cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class YourImplementation(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
pass # your implementation goes here
Hope you find it useful!
I think the solution proposed by @tahesse can raise a dead lock. If the __init__
method contains another singleton than the lock won’t be released.
Foe example :
class ThreadSafeSingleton(type):
_instances = {}
_singleton_lock = threading.Lock()
def __call__(cls, *args, **kwargs):
# double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
if cls not in cls._instances:
with cls._singleton_lock:
if cls not in cls._instances:
cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class YourImplementation1(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
pass # your implementation goes here
def simple_method(self):
return "this is a test"
class YourImplementation2(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
self.your_implementation1 = YourImplementation1()
def simple_method(self):
print(self.your_implementation1.simple_method())
So I changed that solution a bit
class ThreadSafeSingleton(type):
_instances = {}
_singleton_locks: Dict[Any, threading.Lock] = {}
def __call__(cls, *args, **kwargs):
# double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
if cls not in cls._instances:
if cls not in cls._singleton_locks:
cls._singleton_locks[cls] = threading.Lock()
with cls._singleton_locks[cls]:
if cls not in cls._instances:
cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class YourImplementation1(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
pass # your implementation goes here
def simple_method(self):
return "this is a test"
class YourImplementation2(metaclass=ThreadSafeSingleton):
def __init__(self, *args, **kwargs):
self.your_implementation1 =YourImplementation1()
def simple_method(self):
print(self.your_implementation1.simple_method())