Python fcntl.lockf() Examples
The following are 30
code examples of fcntl.lockf().
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
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: util.py From wechat-alfred-workflow with MIT License | 6 votes |
def release(self): """Release the lock by deleting `self.lockfile`.""" if not self._lock.is_set(): return False try: fcntl.lockf(self._lockfile, fcntl.LOCK_UN) except IOError: # pragma: no cover pass finally: self._lock.clear() self._lockfile = None try: os.unlink(self.lockfile) except (IOError, OSError): # pragma: no cover pass return True
Example #2
Source File: bounce_lib.py From paasta with Apache License 2.0 | 6 votes |
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 #3
Source File: server.py From chia-blockchain with Apache License 2.0 | 6 votes |
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 #4
Source File: package.py From builds with GNU General Public License v3.0 | 6 votes |
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 #5
Source File: util.py From alfred-brightness with MIT License | 6 votes |
def release(self): """Release the lock by deleting `self.lockfile`.""" if not self._lock.is_set(): return False try: fcntl.lockf(self._lockfile, fcntl.LOCK_UN) except IOError: # pragma: no cover pass finally: self._lock.clear() self._lockfile = None try: os.unlink(self.lockfile) except (IOError, OSError): # pragma: no cover pass return True
Example #6
Source File: singleinstance.py From aw-client with Mozilla Public License 2.0 | 6 votes |
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: pcache.py From rucio with Apache License 2.0 | 6 votes |
def unlock_file(self, name): f = self.locks.get(name) if not f: self.log(DEBUG, "unlock_file: %s not locked", name) return # XXXX does this create a possible race condition? if 0: try: os.unlink(name) except: pass status = fcntl.lockf(f, fcntl.LOCK_UN) f.close() del self.locks[name] return status
Example #8
Source File: util.py From gist-alfred with MIT License | 6 votes |
def release(self): """Release the lock by deleting `self.lockfile`.""" if not self._lock.is_set(): return False try: fcntl.lockf(self._lockfile, fcntl.LOCK_UN) except IOError: # pragma: no cover pass finally: self._lock.clear() self._lockfile = None try: os.unlink(self.lockfile) except (IOError, OSError): # pragma: no cover pass return True
Example #9
Source File: local_mode_utils.py From sagemaker-pytorch-inference-toolkit with Apache License 2.0 | 5 votes |
def lock(): # Since Local Mode uses the same port for serving, we need a lock in order # to allow concurrent test execution. local_mode_lock_fd = open(LOCK_PATH, 'w') local_mode_lock = local_mode_lock_fd.fileno() fcntl.lockf(local_mode_lock, fcntl.LOCK_EX) try: yield finally: time.sleep(5) fcntl.lockf(local_mode_lock, fcntl.LOCK_UN)
Example #10
Source File: root_cache.py From mock with GNU General Public License v2.0 | 5 votes |
def _rootCacheUnlock(self): fcntl.lockf(self.rootCacheLock.fileno(), fcntl.LOCK_UN)
Example #11
Source File: root_cache.py From mock with GNU General Public License v2.0 | 5 votes |
def _rootCacheLock(self, shared=1): lockType = fcntl.LOCK_EX if shared: lockType = fcntl.LOCK_SH try: fcntl.lockf(self.rootCacheLock.fileno(), lockType | fcntl.LOCK_NB) except IOError: self.state.start("Waiting for rootcache lock") fcntl.lockf(self.rootCacheLock.fileno(), lockType) self.state.finish("Waiting for rootcache lock")
Example #12
Source File: daemon.py From shadowsocksR-b with Apache License 2.0 | 5 votes |
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 #13
Source File: utils.py From kitty with GNU General Public License v3.0 | 5 votes |
def single_instance_unix(name: str) -> bool: import socket for path in unix_socket_paths(name): socket_path = path.rpartition('.')[0] + '.sock' fd = os.open(path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC | os.O_CLOEXEC) try: fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except OSError as err: if err.errno in (errno.EAGAIN, errno.EACCES): # Client s = socket.socket(family=socket.AF_UNIX) s.connect(socket_path) single_instance.socket = s return False raise s = socket.socket(family=socket.AF_UNIX) try: s.bind(socket_path) except OSError as err: if err.errno in (errno.EADDRINUSE, errno.EEXIST): os.unlink(socket_path) s.bind(socket_path) else: raise single_instance.socket = s # prevent garbage collection from closing the socket atexit.register(remove_socket_file, s, socket_path) s.listen() s.set_inheritable(False) return True return False
Example #14
Source File: package.py From builds with GNU General Public License v3.0 | 5 votes |
def unlock(self): """ Unlocks the package to allow other processes to operate on its shared resources. """ if not self.locking_enabled: return LOG.debug("Unlocking file {}".format(self.lock_file_path)) fcntl.lockf(self.lock_file, fcntl.LOCK_UN) self.lock_file.close()
Example #15
Source File: daemon.py From ssr-ml with Apache License 2.0 | 5 votes |
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 #16
Source File: logs.py From sonic_contest with MIT License | 5 votes |
def _set_locked(self, locked): if not self._use_locking: return import fcntl if locked: fcntl.lockf(self._file_desc, fcntl.LOCK_EX) else: fcntl.lockf(self._file_desc, fcntl.LOCK_UN)
Example #17
Source File: util.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def release(self): """Release the lock""" if self.lock_file: if fcntl: fcntl.lockf(self.lock_file, fcntl.LOCK_UN) self.lock_file.close() self.lock_file = None
Example #18
Source File: daemon.py From shadowsocks with Apache License 2.0 | 5 votes |
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 #19
Source File: mailbox.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def _unlock_file(f): """Unlock file f using lockf and dot locking.""" if fcntl: fcntl.lockf(f, fcntl.LOCK_UN) if os.path.exists(f.name + '.lock'): os.remove(f.name + '.lock')
Example #20
Source File: mailbox.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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 #21
Source File: passthrough.py From gitfs with Apache License 2.0 | 5 votes |
def release(self, path, fh): fcntl.lockf(fh, fcntl.LOCK_UN)
Example #22
Source File: passthrough.py From gitfs with Apache License 2.0 | 5 votes |
def lock(self, path, fh, cmd, lock): fcntl.lockf(fh, fcntl.LOCK_EX)
Example #23
Source File: _fcntl_opener.py From aqua-monitor with GNU Lesser General Public License v3.0 | 5 votes |
def unlock_and_close(self): """Close and unlock the file using the fcntl.lockf primitive.""" if self._locked: fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN) self._locked = False if self._fh: self._fh.close()
Example #24
Source File: time-machine.py From rsync-time-machine with GNU General Public License v2.0 | 5 votes |
def flock_release(fd): ''' Release lock so next snapshots can continue ''' fcntl.lockf(fd, fcntl.LOCK_UN) os.close(fd) os.remove(cfg['lock_file'])
Example #25
Source File: time-machine.py From rsync-time-machine with GNU General Public License v2.0 | 5 votes |
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 #26
Source File: helpers.py From appetite with Apache License 2.0 | 5 votes |
def __exit__(self, type, value, tb): # pylint: disable=redefined-builtin """Exit RunSingleInstance class :return: None """ try: if not self.__is_running: fcntl.lockf(self.__filelock, fcntl.LOCK_UN) self.__filelock.close() os.unlink(self.__lockfile) except Exception as err: logger.error("Error unlocking single instance file", error=err.message)
Example #27
Source File: helpers.py From appetite with Apache License 2.0 | 5 votes |
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 #28
Source File: daemon.py From shadowsocksr with Apache License 2.0 | 5 votes |
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 #29
Source File: UserFile.py From pycopia with Apache License 2.0 | 5 votes |
def _lock(self, flag, length, start, whence): fcntl.lockf(self.fileno(), flag, length, start, whence)
Example #30
Source File: _fcntl_opener.py From alfred-gmail with MIT License | 5 votes |
def unlock_and_close(self): """Close and unlock the file using the fcntl.lockf primitive.""" if self._locked: fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN) self._locked = False if self._fh: self._fh.close()