class Singleton(type): _instances = {} _semaphores = lockutils.Semaphores() def __call__(cls, *args, **kwargs): with lockutils.lock('singleton_lock', semaphores=cls._semaphores): if cls not in cls._instances: cls._instances[cls] = super( Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls]
def test_lock_internally_different_collections(self): s1 = lockutils.Semaphores() s2 = lockutils.Semaphores() trigger = threading.Event() who_ran = collections.deque() def f(name, semaphores, pull_trigger): with lockutils.internal_lock('testing', semaphores=semaphores): if pull_trigger: trigger.set() else: trigger.wait() who_ran.append(name) threads = [ threading.Thread(target=f, args=(1, s1, True)), threading.Thread(target=f, args=(2, s2, False)), ] for thread in threads: thread.start() for thread in threads: thread.join() self.assertEqual([1, 2], sorted(who_ran))
def setUp(self): super(BaseTest, self).setUp() init_test_conf() self.session = db.get_session() engine = db.get_engine() db.Base.metadata.create_all(engine) engine.connect() self.addCleanup(db.get_engine().dispose) plugins_base._HOOKS_MGR = None node_cache._SEMAPHORES = lockutils.Semaphores() for name in ('_', '_LI', '_LW', '_LE', '_LC'): patch = mock.patch.object(i18n, name, lambda s: s) patch.start() # 'p=patch' magic is due to how closures work self.addCleanup(lambda p=patch: p.stop())
def setUp(self): super(BaseTest, self).setUp() if not self.IS_FUNCTIONAL: self.init_test_conf() self.session = db.get_writer_session() engine = self.session.get_bind() db.Base.metadata.create_all(engine) engine.connect() self.addCleanup(engine.dispose) plugins_base._HOOKS_MGR = None node_cache._SEMAPHORES = lockutils.Semaphores() patch = mock.patch.object(i18n, '_', lambda s: s) patch.start() # 'p=patch' magic is due to how closures work self.addCleanup(lambda p=patch: p.stop()) utils._EXECUTOR = futurist.SynchronousExecutor(green=True)
from sqlalchemy.orm import exc as orm_errors from sqlalchemy import text from ironic_inspector.common.i18n import _ from ironic_inspector.common import ironic as ir_utils from ironic_inspector import db from ironic_inspector import introspection_state as istate from ironic_inspector import utils CONF = cfg.CONF LOG = utils.getProcessingLogger(__name__) MACS_ATTRIBUTE = 'mac' _LOCK_TEMPLATE = 'node-%s' _SEMAPHORES = lockutils.Semaphores() def _get_lock(uuid): """Get lock object for a given node UUID.""" return lockutils.internal_lock(_LOCK_TEMPLATE % uuid, semaphores=_SEMAPHORES) def _get_lock_ctx(uuid): """Get context manager yielding a lock object for a given node UUID.""" return lockutils.lock(_LOCK_TEMPLATE % uuid, semaphores=_SEMAPHORES) class NodeInfo(object): """Record about a node in the cache.
def do_test(): s = lockutils.Semaphores() with lockutils.lock('testlock', 'test-', semaphores=s, fair=True): pass