Python fcntl.LOCK_NB Examples

The following are 30 code examples of fcntl.LOCK_NB(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fcntl , or try the search function .
Example #1
Source File: flock.py    From deimos with Apache License 2.0 8 votes vote down vote up
def lock(self):
        if self.handle is None or self.handle.closed:
            self.handle = open(self.path, "w+")
            self.fd = self.handle.fileno()
        if (self.flags & fcntl.LOCK_NB) != 0 or self.seconds is None:
            try:
                fcntl.flock(self.handle, self.flags)
            except IOError as e:
                if e.errno not in [errno.EACCES, errno.EAGAIN]:
                    raise e
                raise Locked(self.path)
        else:
            with timeout(self.seconds):
                try:
                    fcntl.flock(self.handle, self.flags)
                except IOError as e:
                    errnos = [errno.EINTR, errno.EACCES, errno.EAGAIN]
                    if e.errno not in errnos:
                        raise e
                    raise Timeout(self.path) 
Example #2
Source File: daemon.py    From smarthome with GNU General Public License v3.0 7 votes vote down vote up
def write_pidfile(pid, pidfile):
    """
    This method writes the PID to the pidfile and locks it while the process is running.

    :param pid: PID of SmartHomeNG
    :param pidfile: Name of the pidfile to write to
    :type pid: int
    :type pidfile: str
    """

    fd = open(pidfile, 'w+')
    fd.write("%s" % pid)
    fd.close()

    # lock pidfile:
    try:
        fd = os.open(pidfile, os.O_RDONLY)
        fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
        # don't close fd or lock is gone
    except OSError as e:
        print("Could not lock pid file: %d (%s)" % (e.errno, e.strerror) , file=sys.stderr) 
Example #3
Source File: utils.py    From IDontSpeakSSL with GNU General Public License v3.0 6 votes vote down vote up
def udpate_status(status_file_path, module_data, module):
	lock_file_path = "{}.lock".format(status_file_path)
	while True:
		status_file_lock = open(lock_file_path, 'r')
		try:
			fcntl.flock(status_file_lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
			with open(status_file_path, "r+") as status_file:
				data = json.load(status_file)
				status_file.seek(0)
				data[module].update(module_data)
				json.dump(data, status_file)
			fcntl.flock(status_file_lock, fcntl.LOCK_UN)
			return
		except:
			time.sleep(0.1)
		status_file_lock.close() 
Example #4
Source File: eddn.py    From EDMarketConnector with GNU General Public License v2.0 6 votes vote down vote up
def load(self):
        # Try to obtain exclusive access to the journal cache
        filename = join(config.app_dir, 'replay.jsonl')
        try:
            try:
                # Try to open existing file
                self.replayfile = open(filename, 'r+', buffering=1)
            except:
                if exists(filename):
                    raise	# Couldn't open existing file
                else:
                    self.replayfile = open(filename, 'w+', buffering=1)	# Create file
            if sys.platform != 'win32':	# open for writing is automatically exclusive on Windows
                lockf(self.replayfile, LOCK_EX|LOCK_NB)
        except:
            if __debug__: print_exc()
            if self.replayfile:
                self.replayfile.close()
            self.replayfile = None
            return False
        self.replaylog = [line.strip() for line in self.replayfile]
        return True 
Example #5
Source File: docker.py    From deimos with Apache License 2.0 6 votes vote down vote up
def containers(self, *args):
        log.info(" ".join(args))
        data = Run(data=True)(deimos.docker.docker("ps", "--no-trunc", "-q"))
        mesos_ids = []
        for line in data.splitlines():
            cid = line.strip()
            state = deimos.state.State(self.state_root, docker_id=cid)
            if not state.exists():
                continue
            try:
                state.lock("wait", LOCK_SH | LOCK_NB)
            except deimos.flock.Err:     # LOCK_EX held, so launch() is running
                mesos_ids += [state.mesos_container_id()]
        containers = Containers()
        for mesos_id in mesos_ids:
            container = containers.containers.add()
            container.value = mesos_id
        recordio.writeProto(containers)
        return 0 
Example #6
Source File: singleinstance.py    From aw-client with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self, client_name):
        self.lockfile = os.path.join(get_cache_dir("client_locks"), client_name)
        logger.debug("SingleInstance lockfile: " + self.lockfile)
        if sys.platform == 'win32':
            try:
                # file already exists, we try to remove (in case previous execution was interrupted)
                if os.path.exists(self.lockfile):
                    os.unlink(self.lockfile)
                    self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
            except OSError as e:
                if e.errno == 13:
                    logger.error("Another instance is already running, quitting.")
                    sys.exit(-1)
                else:
                    raise e
        else:  # non Windows
            self.fp = open(self.lockfile, 'w')
            try:
                fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except IOError:
                logger.error("Another instance is already running, quitting.")
                sys.exit(-1) 
Example #7
Source File: flock.py    From deimos with Apache License 2.0 6 votes vote down vote up
def __init__(self, path, flags, seconds=default_timeout):
        """Construct a lockable file handle. Handles are recycled.

        If seconds is 0, LOCK_NB will be set. If LOCK_NB is set, seconds will
        be set to 0. If seconds is None, there will be no timeout; but flags
        will not be adjusted in any way.
        """
        full = os.path.abspath(path)
        flags, seconds = nb_seconds(flags, seconds)
        if full not in locks:
            _Struct.__init__(self, path=full,
                                   handle=None,
                                   fd=None,
                                   flags=flags,
                                   seconds=seconds)
            locks[self.path] = self 
Example #8
Source File: utils.py    From django-gateone with GNU General Public License v3.0 6 votes vote down vote up
def write_pid(path):
    """Writes our PID to *path*."""
    try:
        pid = os.getpid()
        with io.open(path, mode='w', encoding='utf-8') as pidfile:
            # Get a non-blocking exclusive lock
            fcntl.flock(pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
            pidfile.seek(0)
            pidfile.truncate(0)
            pidfile.write(unicode(pid))
    except:
        logging.error(_("Could not write PID file: %s") % path)
        raise # This raises the original exception
    finally:
        try:
            pidfile.close()
        except:
            pass 
Example #9
Source File: server.py    From chia-blockchain with Apache License 2.0 6 votes vote down vote up
def singleton(lockfile, text="semaphore"):
    """
    Open a lockfile exclusively.
    """

    if not lockfile.parent.exists():
        mkdir(lockfile.parent)

    try:
        if has_fcntl:
            f = open(lockfile, "w")
            fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
        else:
            if lockfile.exists():
                lockfile.unlink()
            fd = os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
            f = open(fd, "w")
        f.write(text)
    except IOError:
        return None
    return f 
Example #10
Source File: package.py    From builds with GNU General Public License v3.0 6 votes vote down vote up
def lock(self):
        """
        Locks the package to avoid concurrent operations on its shared
        resources.
        Currently, the only resource shared among scripts executed from
        different directories is the repository.
        """
        if not self.locking_enabled:
            LOG.debug("This package has no shared resources to lock")
            return

        LOG.debug("Checking for lock on file {}.".format(self.lock_file_path))
        self.lock_file = open(self.lock_file_path, "w")
        try:
            fcntl.lockf(self.lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError as exc:
            RESOURCE_UNAVAILABLE_ERROR = 11
            if exc.errno == RESOURCE_UNAVAILABLE_ERROR:
                LOG.info("Waiting for other process to finish operations "
                         "on {}.".format(self.name))
            else:
                raise
        fcntl.lockf(self.lock_file, fcntl.LOCK_EX) 
Example #11
Source File: updater.py    From open_dnsdb with Apache License 2.0 6 votes vote down vote up
def _create_pid_file():
    global fp
    pidfile = CONF.etc.pidfile
    if pidfile is None:
        raise UpdaterErr("No pidfile option found in config file.")
    try:
        fp = open(pidfile, 'w')
        # LOCK_EX    /* exclusive lock */
        # LOCK_NB   * don't block when locking */
        fcntl.flock(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
        fp.truncate()
        pid = os.getpid()
        fp.write(str(pid))
        fp.flush()
    except Exception as e:
        raise UpdaterErr("Failed to lock pidfile, perhaps named_updater is already running.") 
Example #12
Source File: test_bounce_lib.py    From paasta with Apache License 2.0 6 votes vote down vote up
def test_bounce_lock(self):
        import fcntl

        lock_name = "the_internet"
        lock_file = "/var/lock/%s.lock" % lock_name
        fake_fd = mock.mock_open()
        with mock.patch("builtins.open", fake_fd, autospec=None) as open_patch:
            with mock.patch("fcntl.lockf", autospec=None) as lockf_patch:
                with mock.patch("os.remove", autospec=None) as remove_patch:
                    with bounce_lib.bounce_lock(lock_name):
                        pass
        open_patch.assert_called_once_with(lock_file, "w")
        lockf_patch.assert_called_once_with(
            fake_fd.return_value, fcntl.LOCK_EX | fcntl.LOCK_NB
        )
        fake_fd.return_value.__exit__.assert_called_once_with(None, None, None)
        remove_patch.assert_called_once_with(lock_file) 
Example #13
Source File: bounce_lib.py    From paasta with Apache License 2.0 6 votes vote down vote up
def bounce_lock(name):
    """Acquire a bounce lockfile for the name given. The name should generally
    be the service namespace being bounced.

    This is a contextmanager. Please use it via 'with bounce_lock(name):'.

    :param name: The lock name to acquire"""
    lockfile = "/var/lock/%s.lock" % name
    with open(lockfile, "w") as fd:
        remove = False
        try:
            fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
            remove = True
            yield
        except IOError:
            raise LockHeldException("Service %s is already being bounced!" % name)
        finally:
            if remove:
                os.remove(lockfile) 
Example #14
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def AcquireFileLock(target_file, flags):
  """ Lock the target file. Note that if |target_file| is closed, the lock is
    automatically released.
  Args:
    target_file: file handle of the file to acquire lock.
    flags: can be any of the type LOCK_EX, LOCK_SH, LOCK_NB, or a bitwise
      OR combination of flags.
  """
  assert flags in (
      LOCK_EX, LOCK_SH, LOCK_NB, LOCK_EX | LOCK_NB, LOCK_SH | LOCK_NB)
  if os.name == 'nt':
    _LockImplWin(target_file, flags)
  elif os.name == 'posix':
    _LockImplPosix(target_file, flags)
  else:
    raise NotImplementedError('%s is not supported' % os.name) 
Example #15
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def AcquireFileLock(target_file, flags):
  """ Lock the target file. Note that if |target_file| is closed, the lock is
    automatically released.
  Args:
    target_file: file handle of the file to acquire lock.
    flags: can be any of the type LOCK_EX, LOCK_SH, LOCK_NB, or a bitwise
      OR combination of flags.
  """
  assert flags in (
      LOCK_EX, LOCK_SH, LOCK_NB, LOCK_EX | LOCK_NB, LOCK_SH | LOCK_NB)
  if os.name == 'nt':
    _LockImplWin(target_file, flags)
  elif os.name == 'posix':
    _LockImplPosix(target_file, flags)
  else:
    raise NotImplementedError('%s is not supported' % os.name) 
Example #16
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def AcquireFileLock(target_file, flags):
  """ Lock the target file. Note that if |target_file| is closed, the lock is
    automatically released.
  Args:
    target_file: file handle of the file to acquire lock.
    flags: can be any of the type LOCK_EX, LOCK_SH, LOCK_NB, or a bitwise
      OR combination of flags.
  """
  assert flags in (
      LOCK_EX, LOCK_SH, LOCK_NB, LOCK_EX | LOCK_NB, LOCK_SH | LOCK_NB)
  if os.name == 'nt':
    _LockImplWin(target_file, flags)
  elif os.name == 'posix':
    _LockImplPosix(target_file, flags)
  else:
    raise NotImplementedError('%s is not supported' % os.name) 
Example #17
Source File: pidfile.py    From pscheduler with Apache License 2.0 6 votes vote down vote up
def __enter__(self):
        if self.path is None:
            return self.pidfile

        self.pidfile = open(self.path, "a+")
        try:
            fcntl.flock(self.pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            self.pidfile = None
            raise SystemExit("Already running according to " + self.path)
        self.pidfile.seek(0)
        self.pidfile.truncate()
        self.pidfile.write(str(os.getpid()))
        self.pidfile.flush()
        self.pidfile.seek(0)
        return self.pidfile 
Example #18
Source File: utils.py    From pytos with Apache License 2.0 6 votes vote down vote up
def acquire(self, blocking=None):
        # Give an opportunity to set blocking with the class for context use
        if blocking is None:
            blocking = self.blocking

        if blocking:
            lock_mode = fcntl.LOCK_EX
        else:
            lock_mode = fcntl.LOCK_EX | fcntl.LOCK_NB
        if self.lock_file.closed:
            self._get_lock_file_handle()
        if not self.locked:
            try:
                self.lock = fcntl.flock(self.lock_file, lock_mode)
                self.locked = True
            except IOError:
                raise IOError("File '{}' is already locked.".format(self.lock_file_name))
        else:
            raise IOError("File '{}' is already locked.".format(self.lock_file_name)) 
Example #19
Source File: daemon.py    From ssr-ml with Apache License 2.0 5 votes vote down vote up
def write_pid_file(pid_file, pid):
    import fcntl
    import stat

    try:
        fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
                     stat.S_IRUSR | stat.S_IWUSR)
    except OSError as e:
        shell.print_exception(e)
        return -1
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    assert flags != -1
    flags |= fcntl.FD_CLOEXEC
    r = fcntl.fcntl(fd, fcntl.F_SETFD, flags)
    assert r != -1
    # There is no platform independent way to implement fcntl(fd, F_SETLK, &fl)
    # via fcntl.fcntl. So use lockf instead
    try:
        fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, 0, 0, os.SEEK_SET)
    except IOError:
        r = os.read(fd, 32)
        if r:
            logging.error('already started at pid %s' % common.to_str(r))
        else:
            logging.error('already started')
        os.close(fd)
        return -1
    os.ftruncate(fd, 0)
    os.write(fd, common.to_bytes(str(pid)))
    return 0 
Example #20
Source File: filelock.py    From smartdispatch with Do What The F*ck You Want To Public License 5 votes vote down vote up
def open_with_flock(*args, **kwargs):
    """ Context manager for opening file with an exclusive lock. """
    f = open(*args, **kwargs)
    try:
        fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError:
        no_attempt = 0
        while no_attempt < MAX_ATTEMPTS:
            try:
                logging.info("Can't immediately write-lock the file ({0}), waiting.".format(f.name))
                start_time = time.time()
                fcntl.lockf(f, fcntl.LOCK_EX)
                break
            except IOError as e:
                if e.errno == errno.EDEADLK:
                    logging.warn("The OS complained because the process have been waiting on the lockf for {0} sec with the error ({1}: {2}). Retrying. ".format(time.time() - start_time, e.errno, e.strerror))
                    f.close()
                    time.sleep(TIME_BETWEEN_ATTEMPTS)
                    f = open(*args, **kwargs)
                    no_attempt += 1
                else:
                    raise e

        if no_attempt == MAX_ATTEMPTS:
            raise IOError("Failed to lock {0} {1} times.".format(f.name, MAX_ATTEMPTS))

    try:
        yield f
    finally:
        fcntl.lockf(f, fcntl.LOCK_UN)
        f.close() 
Example #21
Source File: helpers.py    From appetite with Apache License 2.0 5 votes vote down vote up
def __enter__(self):
        """Enter RunSingleInstance class
        :return: self
        """
        self.__checked = True

        try:
            self.__filelock = open(self.__lockfile, 'w+')
            # None blocking lock
            fcntl.lockf(self.__filelock, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            if self.__filelock is not None:
                self.__is_running = True
        return self 
Example #22
Source File: daemon.py    From smarthome with GNU General Public License v3.0 5 votes vote down vote up
def check_sh_is_running(pidfile):
    """
    This method checks whether another smarthome process process is already running.

    :param pidfile: Name of the pidfile to check
    :type pidfile: str

    :return: True: if SmartHomeNG is running, False: if SmartHome is not running
    :rtype: bool
    """

    pid = read_pidfile(pidfile)
    isRunning = False
    if pid > 0 and psutil.pid_exists(pid):
        try:
            fd = os.open(pidfile, os.O_RDONLY)
            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
            # pidfile not locked, so sh is terminated
        except OSError as e:
            if (e.errno == errno.EWOULDBLOCK):
                # pidfile is locked, so sh is running
                isRunning = True
            else:
                print("Error while testing lock in pidfile %s: %d (%s)" % (pidfile, e.errno, e.strerror) , file=sys.stderr)
                sys.exit(1)
        finally:
            if fd:
                os.close(fd)
    return isRunning 
Example #23
Source File: posix.py    From pid with Apache License 2.0 5 votes vote down vote up
def _flock(self, fileno):
        fcntl.flock(self.fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) 
Example #24
Source File: daemon.py    From shadowsocks with Apache License 2.0 5 votes vote down vote up
def write_pid_file(pid_file, pid):
    import fcntl
    import stat

    try:
        fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
                     stat.S_IRUSR | stat.S_IWUSR)
    except OSError as e:
        shell.print_exception(e)
        return -1
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    assert flags != -1
    flags |= fcntl.FD_CLOEXEC
    r = fcntl.fcntl(fd, fcntl.F_SETFD, flags)
    assert r != -1
    # There is no platform independent way to implement fcntl(fd, F_SETLK, &fl)
    # via fcntl.fcntl. So use lockf instead
    try:
        fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB, 0, 0, os.SEEK_SET)
    except IOError:
        r = os.read(fd, 32)
        if r:
            logging.error('already started at pid %s' % common.to_str(r))
        else:
            logging.error('already started')
        os.close(fd)
        return -1
    os.ftruncate(fd, 0)
    os.write(fd, common.to_bytes(str(pid)))
    return 0 
Example #25
Source File: gcs_config.py    From gokart with MIT License 5 votes vote down vote up
def get_gcs_client(self) -> luigi.contrib.gcs.GCSClient:
        if (not os.path.isfile(self.discover_cache_local_path)):
            with open(self.discover_cache_local_path, "w") as f:
                try:
                    fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)

                    params = {"api": "storage", "apiVersion": "v1"}
                    discovery_http = build_http()
                    for discovery_url in (self._DISCOVERY_URI, self._V2_DISCOVERY_URI):
                        requested_url = uritemplate.expand(discovery_url, params)
                        try:
                            content = _retrieve_discovery_doc(
                                requested_url, discovery_http, False
                            )
                        except HttpError as e:
                            if e.resp.status == http_client.NOT_FOUND:
                                continue
                            else:
                                raise e
                        break
                    f.write(content)
                    fcntl.flock(f, fcntl.LOCK_UN)
                except IOError:
                    # try to read
                    pass

        with open(self.discover_cache_local_path, "r") as f:
            fcntl.flock(f, fcntl.LOCK_SH)
            descriptor = f.read()
            fcntl.flock(f, fcntl.LOCK_UN)
            return luigi.contrib.gcs.GCSClient(oauth_credentials=self._load_oauth_credentials(), descriptor=descriptor) 
Example #26
Source File: filelock.py    From veros with MIT License 5 votes vote down vote up
def _acquire(self):
        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
        fd = os.open(self._lock_file, open_mode)

        try:
            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except (IOError, OSError):
            os.close(fd)
        else:
            self._lock_file_fd = fd
        return None 
Example #27
Source File: time-machine.py    From rsync-time-machine with GNU General Public License v2.0 5 votes vote down vote up
def flock_exclusive():
    ''' lock so only one snapshot of current config is running '''
    fd = os.open(cfg['lock_file'], os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0600)
    try:
        fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError:
        return False
    return fd 
Example #28
Source File: mailbox.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def _lock_file(f, dotlock=True):
    """Lock file f using lockf and dot locking."""
    dotlock_done = False
    try:
        if fcntl:
            try:
                fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except OSError as e:
                if e.errno in (errno.EAGAIN, errno.EACCES, errno.EROFS):
                    raise ExternalClashError('lockf: lock unavailable: %s' %
                                             f.name)
                else:
                    raise
        if dotlock:
            try:
                pre_lock = _create_temporary(f.name + '.lock')
                pre_lock.close()
            except OSError as e:
                if e.errno in (errno.EACCES, errno.EROFS):
                    return  # Without write access, just skip dotlocking.
                else:
                    raise
            try:
                if hasattr(os, 'link'):
                    os.link(pre_lock.name, f.name + '.lock')
                    dotlock_done = True
                    os.unlink(pre_lock.name)
                else:
                    os.rename(pre_lock.name, f.name + '.lock')
                    dotlock_done = True
            except FileExistsError:
                os.remove(pre_lock.name)
                raise ExternalClashError('dot lock unavailable: %s' %
                                         f.name)
    except:
        if fcntl:
            fcntl.lockf(f, fcntl.LOCK_UN)
        if dotlock_done:
            os.remove(f.name + '.lock')
        raise 
Example #29
Source File: mac_address.py    From paasta with Apache License 2.0 5 votes vote down vote up
def obtain_lock(lock_filepath):
    """ Open and obtain a flock on the parameter. Returns a file if successful, None if not
    """
    lock_file = open(lock_filepath, "w")
    try:
        fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
        return lock_file
    except IOError as err:
        if err.errno != errno.EAGAIN:
            raise
        lock_file.close()
        return None 
Example #30
Source File: iptables_raw.py    From mix-deploy-example with Apache License 2.0 5 votes vote down vote up
def acquire_lock_or_exit(self, wait_for_seconds=10):
        lock_file = self.STATE_DIR + '/.iptables.lock'
        i = 0
        f = open(lock_file, 'w+')
        while i < wait_for_seconds:
            try:
                fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
                return
            except IOError:
                i += 1
                time.sleep(1)
        Iptables.module.fail_json(msg="Could not acquire lock to continue execution! "
                                      "Probably another instance of this module is running.")

    # Check if a table has anything to flush (to check all tables pass table='*').