MCPcopy Create free account
hub / github.com/catboost/catboost / Singleton

Class Singleton

library/python/func/__init__.py:152–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

150
151
152class Singleton(type):
153 __instances = {}
154 __lock = threading.Lock()
155
156 class _LockedObj:
157 def __init__(self, obj, lock):
158 self.obj = obj
159 self.lock = lock
160
161 def __call__(cls, *args, **kwargs):
162 if cls not in cls.__instances:
163 with cls.__lock:
164 if cls not in cls.__instances:
165 cls.__instances[cls] = cls._LockedObj(None, threading.Lock())
166
167 if not cls.__instances[cls].obj:
168 with cls.__instances[cls].lock:
169 if not cls.__instances[cls].obj:
170 cls.__instances[cls].obj = super(Singleton, cls).__call__(*args, **kwargs)
171 return cls.__instances[cls].obj
172
173
174def stable_uniq(it):

Callers

nothing calls this directly

Calls 1

LockMethod · 0.45

Tested by

no test coverage detected