def __init__(self, kind, value, maxvalue, name=None): # unlink_now is only used on win32 or when we are using fork. unlink_now = False if name is None: # Try to find an unused name for the SemLock instance. for i in range(100): try: self._semlock = _SemLock(kind, value, maxvalue, SemLock._make_name(), unlink_now) except FileExistsError: # pragma: no cover pass else: break else: # pragma: no cover raise FileExistsError('cannot find name for semaphore') else: self._semlock = _SemLock(kind, value, maxvalue, name, unlink_now) self.name = name util.debug('created semlock with handle %s and name "%s"' % (self._semlock.handle, self.name)) self._make_methods() def _after_fork(obj): obj._semlock._after_fork() util.register_after_fork(self, _after_fork) # When the object is garbage collected or the # process shuts down we unlink the semaphore name resource_tracker.register(self._semlock.name, "semlock") util.Finalize(self, SemLock._cleanup, (self._semlock.name, ), exitpriority=0)
def resource_exists(name, rtype): if rtype in ["folder", "file"]: return os.path.exists(name) elif rtype == "semlock": try: _SemLock(1, 1, 1, name, False) sem_unlink(name) return False except OSError: return True else: raise ValueError(f"Resource type {rtype} not understood")
def create_resource(rtype): if rtype == "folder": return mkdtemp(dir=os.getcwd()) elif rtype == "semlock": name = f"test-loky-{os.getpid()}-{next(_rand_name)}" _SemLock(1, 1, 1, name, False) return name elif rtype == "file": tmpfile = NamedTemporaryFile(delete=False) tmpfile.close() return tmpfile.name else: raise ValueError(f"Resource type {rtype} not understood")
def __init__(self, kind, value, maxvalue): # unlink_now is only used on win32 or when we are using fork. unlink_now = False for i in range(100): try: self._semlock = _SemLock( kind, value, maxvalue, SemLock._make_name(), unlink_now) except FileExistsError: # pragma: no cover pass else: break else: # pragma: no cover raise FileExistsError('cannot find name for semaphore') util.debug('created semlock with handle %s and name "%s"' % (self._semlock.handle, self._semlock.name)) self._make_methods() def _after_fork(obj): obj._semlock._after_fork() util.register_after_fork(self, _after_fork) # When the object is garbage collected or the # process shuts down we unlink the semaphore name semaphore_tracker.register(self._semlock.name) util.Finalize(self, SemLock._cleanup, (self._semlock.name,), exitpriority=0)