def acquire(self, blocking=True, shared=False): if shared: raise tooz.NotImplemented if (blocking is not True and sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED is False): raise tooz.NotImplemented("This system does not support" " semaphore timeouts") blocking, timeout = utils.convert_blocking(blocking) start_time = None if not blocking: timeout = 0 elif blocking and timeout is not None: start_time = time.time() while True: tmplock = None try: tmplock = sysv_ipc.Semaphore(self.key, flags=sysv_ipc.IPC_CREX, initial_value=1) tmplock.undo = True except sysv_ipc.ExistentialError: # We failed to create it because it already exists, then try to # grab the existing one. try: tmplock = sysv_ipc.Semaphore(self.key) tmplock.undo = True except sysv_ipc.ExistentialError: # Semaphore has been deleted in the mean time, retry from # the beginning! continue if start_time is not None: elapsed = max(0.0, time.time() - start_time) if elapsed >= timeout: # Ran out of time... return False adjusted_timeout = timeout - elapsed else: adjusted_timeout = timeout try: tmplock.acquire(timeout=adjusted_timeout) except sysv_ipc.BusyError: tmplock = None return False except sysv_ipc.ExistentialError: # Likely the lock has been deleted in the meantime, retry continue else: self._lock = tmplock return True
def _start(self): super(RedisDriver, self)._start() try: self._client = self._make_client(self._parsed_url, self._options, self.timeout) except exceptions.RedisError as e: utils.raise_with_cause(coordination.ToozConnectionError, encodeutils.exception_to_unicode(e), cause=e) else: # Ensure that the server is alive and not dead, this does not # ensure the server will always be alive, but does insure that it # at least is alive once... with _translate_failures(): self._server_info = self._client.info() # Validate we have a good enough redis version we are connected # to so that the basic set of features we support will actually # work (instead of blowing up). new_enough, redis_version = self._check_fetch_redis_version( self.MIN_VERSION) if not new_enough: raise tooz.NotImplemented("Redis version greater than or" " equal to '%s' is required" " to use this driver; '%s' is" " being used which is not new" " enough" % (self.MIN_VERSION, redis_version)) tpl_params = { 'group_existence_value': self.GROUP_EXISTS_VALUE, 'group_existence_key': self.GROUP_EXISTS, } # For py3.x ensure these are unicode since the string template # replacement will expect unicode (and we don't want b'' as a # prefix which will happen in py3.x if this is not done). for (k, v) in six.iteritems(tpl_params.copy()): if isinstance(v, six.binary_type): v = v.decode('ascii') tpl_params[k] = v prepared_scripts = {} for name, raw_script_tpl in six.iteritems(self.SCRIPTS): script_tpl = string.Template(raw_script_tpl) script = script_tpl.substitute(**tpl_params) prepared_scripts[name] = self._client.register_script(script) self._scripts = prepared_scripts self.heartbeat() self._started = True