Python fcntl.LOCK_EX Examples
The following are 30
code examples of fcntl.LOCK_EX().
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: daemon.py From smarthome with GNU General Public License v3.0 | 7 votes |
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 #2
Source File: updater.py From open_dnsdb with Apache License 2.0 | 6 votes |
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 #3
Source File: lock.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #4
Source File: mysql_repl_repair2.py From mysql_repl_repair with Apache License 2.0 | 6 votes |
def run(self): "check mysql replication and handle errors" global sigint_up # add file lock first to sure only 1 process is running on this instance if not os.path.exists(self.lockfile): os.system("touch %s" %(self.lockfile)) f = open(self.lockfile, "r") try: fcntl.flock(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB) self.logger.debug("get file lock on %s success" %(self.lockfile)) except Exception, e: msg = "can't get lock for mysql %s, please check script is already running" %(self.port) self.logger.error(msg) sigint_up = True raise Exception(msg)
Example #5
Source File: mysql_repl_repair.py From mysql_repl_repair with Apache License 2.0 | 6 votes |
def run(self): "check mysql replication and handle errors" global sigint_up # add file lock first to sure only 1 process is running on this instance if not os.path.exists(self.lockfile): os.system("touch %s" %(self.lockfile)) f = open(self.lockfile, "r") try: fcntl.flock(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB) self.logger.debug("get file lock on %s success" %(self.lockfile)) except Exception, e: msg = "can't get lock for mysql %s, please check script is already running" %(self.port) self.logger.error(msg) sigint_up = True raise Exception(msg) # check binlog format
Example #6
Source File: gtest_parallel.py From gtest-parallel with Apache License 2.0 | 6 votes |
def __enter__(self): self._fo = open(self._filename, self._mode) # Regardless of opening mode we always seek to the beginning of file. # This simplifies code working with LockedFile and also ensures that # we lock (and unlock below) always the same region in file on win32. self._fo.seek(0) try: if sys.platform == 'win32': # We are locking here fixed location in file to use it as # an exclusive lock on entire file. msvcrt.locking(self._fo.fileno(), msvcrt.LK_LOCK, 1) else: fcntl.flock(self._fo.fileno(), fcntl.LOCK_EX) except IOError: self._fo.close() raise return self._fo
Example #7
Source File: utils.py From IDontSpeakSSL with GNU General Public License v3.0 | 6 votes |
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 #8
Source File: eddn.py From EDMarketConnector with GNU General Public License v2.0 | 6 votes |
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 #9
Source File: docker.py From deimos with Apache License 2.0 | 6 votes |
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 #10
Source File: state.py From deimos with Apache License 2.0 | 6 votes |
def lock(self, name, flags, seconds=60): fmt_time = "indefinite" if seconds is None else "%ds" % seconds fmt_flags = deimos.flock.format_lock_flags(flags) flags, seconds = deimos.flock.nb_seconds(flags, seconds) log.info("request // %s %s (%s)", name, fmt_flags, fmt_time) p = self.resolve(os.path.join("lock", name), mkdir=True) lk = deimos.flock.LK(p, flags, seconds) try: lk.lock() except deimos.flock.Err: log.error("failure // %s %s (%s)", name, fmt_flags, fmt_time) raise if (flags & LOCK_EX) != 0: lk.handle.write(iso() + "\n") log.info("success // %s %s (%s)", name, fmt_flags, fmt_time) return lk
Example #11
Source File: utils.py From pytos with Apache License 2.0 | 6 votes |
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 #12
Source File: pidfile.py From pscheduler with Apache License 2.0 | 6 votes |
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 #13
Source File: util.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def __init__(self, path): self.lock_file = open(path, "w") if fcntl: fcntl.lockf(self.lock_file, fcntl.LOCK_EX)
Example #14
Source File: daemonize.py From pykit with MIT License | 5 votes |
def trylock_or_exit(self, timeout=10): interval = 0.1 n = int(timeout / interval) + 1 flag = fcntl.LOCK_EX | fcntl.LOCK_NB for ii in range(n): fd = os.open(self.lockfile, os.O_RDWR | os.O_CREAT) fcntl.fcntl(fd, fcntl.F_SETFD, fcntl.fcntl(fd, fcntl.F_GETFD, 0) | fcntl.FD_CLOEXEC) try: fcntl.lockf(fd, flag) self.lockfp = os.fdopen(fd, 'w+r') break except IOError as e: os.close(fd) if e[0] == errno.EAGAIN: time.sleep(interval) else: raise else: logger.info("Failure acquiring lock %s" % (self.lockfile, )) sys.exit(1) logger.info("OK acquired lock %s" % (self.lockfile))
Example #15
Source File: iptables_raw.py From elixir-deploy-template with MIT License | 5 votes |
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='*').
Example #16
Source File: multi_model_utils.py From sagemaker-tensorflow-serving-container with Apache License 2.0 | 5 votes |
def lock(path=DEFAULT_LOCK_FILE): f = open(path, 'w') fd = f.fileno() fcntl.lockf(fd, fcntl.LOCK_EX) try: yield finally: time.sleep(1) fcntl.lockf(fd, fcntl.LOCK_UN)
Example #17
Source File: SipLogger.py From b2bua with BSD 2-Clause "Simplified" License | 5 votes |
def do_write(self, obuf): my_flock = flock try: my_flock(self.log, LOCK_EX) except IOError as e: # Catch ENOTSUP if e.args[0] != 45: raise e my_flock = lambda x, y: None try: self.log.write(obuf) self.log.flush() except: pass my_flock(self.log, LOCK_UN)
Example #18
Source File: utils.py From pytos with Apache License 2.0 | 5 votes |
def acquire(self): if self.lock_file.closed: self._get_lock_file_handle() if not self.locked: try: self.lock = fcntl.flock(self.lock_file, fcntl.LOCK_EX) self.locked = True except IOError: raise IOError("Session token file '{}' is already locked.".format(self.file_path)) else: raise IOError("Session token file '{}' is already locked.".format(self.file_path))
Example #19
Source File: ports.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 5 votes |
def AllocateTestServerPort(): """Allocates a port incrementally. Returns: Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used. """ port = 0 ports_tried = [] try: fp_lock = open(_TEST_SERVER_PORT_LOCKFILE, 'w') fcntl.flock(fp_lock, fcntl.LOCK_EX) # Get current valid port and calculate next valid port. if not os.path.exists(_TEST_SERVER_PORT_FILE): ResetTestServerPortAllocation() with open(_TEST_SERVER_PORT_FILE, 'r+') as fp: port = int(fp.read()) ports_tried.append(port) while not IsHostPortAvailable(port): port += 1 ports_tried.append(port) if (port > _TEST_SERVER_PORT_LAST or port < _TEST_SERVER_PORT_FIRST): port = 0 else: fp.seek(0, os.SEEK_SET) fp.write('%d' % (port + 1)) except Exception: # pylint: disable=broad-except logger.exception('Error while allocating port') finally: if fp_lock: fcntl.flock(fp_lock, fcntl.LOCK_UN) fp_lock.close() if port: logger.info('Allocate port %d for test server.', port) else: logger.error('Could not allocate port for test server. ' 'List of ports tried: %s', str(ports_tried)) return port
Example #20
Source File: utils.py From tvalacarta with GNU General Public License v3.0 | 5 votes |
def _lock_file(f, exclusive): fcntl.flock(f, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH)
Example #21
Source File: app.py From qubes-core-admin with GNU Lesser General Public License v2.1 | 5 votes |
def _acquire_lock(self, for_save=False): assert self.__locked_fh is None, 'double lock' while True: try: fd = os.open(self._store, os.O_RDWR | (os.O_CREAT * int(for_save))) except FileNotFoundError: if not for_save: raise qubes.exc.QubesException( 'Qubes XML store {!r} is missing; ' 'use qubes-create tool'.format(self._store)) raise # While we were waiting for lock, someone could have unlink()ed # (or rename()d) our file out of the filesystem. We have to # ensure we got lock on something linked to filesystem. # If not, try again. if os.fstat(fd) != os.stat(self._store): os.close(fd) continue if self.__load_timestamp and \ os.path.getmtime(self._store) != self.__load_timestamp: os.close(fd) raise qubes.exc.QubesException( 'Someone else modified qubes.xml in the meantime') break if os.name == 'posix': fcntl.lockf(fd, fcntl.LOCK_EX) elif os.name == 'nt': # pylint: disable=protected-access overlapped = pywintypes.OVERLAPPED() win32file.LockFileEx( win32file._get_osfhandle(fd), win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped) self.__locked_fh = os.fdopen(fd, 'r+b') return self.__locked_fh
Example #22
Source File: inventory.py From ansible-runner-service with Apache License 2.0 | 5 votes |
def lock(self): fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
Example #23
Source File: mailbox.py From oss-ftp with MIT License | 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 IOError, 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 IOError, 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 OSError, e: if e.errno == errno.EEXIST or \ (os.name == 'os2' and e.errno == errno.EACCES): os.remove(pre_lock.name) raise ExternalClashError('dot lock unavailable: %s' % f.name) else: raise
Example #24
Source File: systemctl3.py From vanilla-docker with MIT License | 5 votes |
def __enter__(self): try: lockfile = self.lockfile() lockname = os.path.basename(lockfile) self.opened = os.open(lockfile, os.O_RDWR | os.O_CREAT, 0o600) for attempt in xrange(int(MaxLockWait or DefaultMaximumTimeout)): try: logg.debug("[%s] %s. trying %s _______ ", os.getpid(), attempt, lockname) fcntl.flock(self.opened, fcntl.LOCK_EX | fcntl.LOCK_NB) st = os.fstat(self.opened) if not st.st_nlink: logg.debug("[%s] %s. %s got deleted, trying again", os.getpid(), attempt, lockname) os.close(self.opened) self.opened = os.open(lockfile, os.O_RDWR | os.O_CREAT, 0o600) continue content = "{ 'systemctl': %s, 'lock': '%s' }\n" % (os.getpid(), lockname) os.write(self.opened, content.encode("utf-8")) logg.debug("[%s] %s. holding lock on %s", os.getpid(), attempt, lockname) return True except BlockingIOError as e: whom = os.read(self.opened, 4096) os.lseek(self.opened, 0, os.SEEK_SET) logg.info("[%s] %s. systemctl locked by %s", os.getpid(), attempt, whom.rstrip()) time.sleep(1) # until MaxLockWait continue logg.error("[%s] not able to get the lock to %s", os.getpid(), lockname) except Exception as e: logg.warning("[%s] oops %s, %s", os.getpid(), str(type(e)), e) #TODO# raise Exception("no lock for %s", self.unit or "global") return False
Example #25
Source File: lock.py From sagemaker-python-sdk with Apache License 2.0 | 5 votes |
def lock(path=DEFAULT_LOCK_PATH): """Create a file lock to control concurrent test execution. Certain tests or test operations need to limit concurrency to work reliably. Examples include local mode endpoint tests and vpc creation tests. """ f = open(path, "w") fd = f.fileno() fcntl.lockf(fd, fcntl.LOCK_EX) try: yield finally: time.sleep(5) fcntl.lockf(fd, fcntl.LOCK_UN)
Example #26
Source File: maildirbot.py From abusehelper with MIT License | 5 votes |
def lockfile(filename): with open(filename, "wb") as opened: fd = opened.fileno() try: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as ioe: if ioe.errno not in (errno.EACCES, errno.EAGAIN): raise yield False else: try: yield True finally: fcntl.flock(fd, fcntl.LOCK_UN)
Example #27
Source File: services.py From abusehelper with MIT License | 5 votes |
def lock_file_nonblocking(fileobj): # Use fcntl.flock instead of fcntl.lockf. lockf on pypy 1.7 seems # to ignore existing locks. try: fcntl.flock(fileobj, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError, ioe: if ioe.errno not in (errno.EACCES, errno.EAGAIN): raise return False
Example #28
Source File: testing.py From xcffib with Apache License 2.0 | 5 votes |
def find_display(): display = 10 while True: f = open(lock_path(display), "w+") try: fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) except OSError: f.close() display += 1 continue return display, f
Example #29
Source File: config.py From aeios with MIT License | 5 votes |
def _acquire(self): """ Unix based locking using fcntl.flock(LOCK_EX | LOCK_NB) """ flags = os.O_RDWR | os.O_CREAT | os.O_TRUNC fd = os.open(self._file, flags, 0644) try: fcntl.flock(fd, fcntl.LOCK_EX|fcntl.LOCK_NB) self._fd = fd except (IOError, OSError): os.close(fd)
Example #30
Source File: server.py From Hunch with Apache License 2.0 | 5 votes |
def lock(lockfile): import fcntl lockfd = open(lockfile, 'w+') fcntl.flock(lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB) return lockfd