Python fcntl.flock() Examples
The following are 30
code examples of fcntl.flock().
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 |
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 |
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: dnx_file_operations.py From dnxfirewall-cmd with GNU General Public License v3.0 | 7 votes |
def __exit__(self, exc_type, exc_val, traceback): if (exc_type is None and self._data_written): os.replace(self._temp_file_path, self._config_file) os.chmod(self._config_file, 0o660) shutil.chown(self._config_file, user=USER, group=GROUP) else: self._temp_file.close() os.unlink(self._temp_file_path) if (self._Log): self._Log.error(f'configuration manager exiting with error: {exc_val}') # releasing lock for purposes specified in flock(1) man page under -u (unlock) fcntl.flock(self._config_lock, fcntl.LOCK_UN) self._config_lock.close() if (self._Log): self._Log.debug(f'file lock released for {self._file_name}') if (exc_type is not ValidationError): return True #will load json data from file, convert it to a python dict, then returned as object
Example #4
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 #5
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 #6
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 #7
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 #8
Source File: flock.py From deimos with Apache License 2.0 | 6 votes |
def lock_browser(directory): bash = """ set -o errexit -o nounset -o pipefail function files_by_inode { find "$1" -type f -printf '%i %p\\n' | LC_ALL=C LANG=C sort } function locking_pids_by_inode { cat /proc/locks | sed -r ' s/^.+ ([^ ]+) +([0-9]+) [^ :]+:[^ :]+:([0-9]+) .+$/\\3 \\2 \\1/ ' | LC_ALL=C LANG=C sort } join <(locking_pids_by_inode) <(files_by_inode "$1") """ subprocess.check_call(["bash", "-c", bash, "bash", os.path.abspath(directory)]) # Thanks to Glenn Maynard # http://stackoverflow.com/questions/5255220/fcntl-flock-how-to-implement-a-timeout/5255473#5255473
Example #9
Source File: file.py From nzb-subliminal with GNU General Public License v3.0 | 6 votes |
def _acquire(self, wait, wrflag, lockflag): wrflag |= os.O_CREAT fileno = os.open(self.filename, wrflag) try: if not wait: lockflag |= self._module.LOCK_NB self._module.flock(fileno, lockflag) except IOError: os.close(fileno) if not wait: # this is typically # "[Errno 35] Resource temporarily unavailable", # because of LOCK_NB return False else: raise else: self._filedescriptor.fileno = fileno return True
Example #10
Source File: file.py From dogpile.cache with MIT License | 6 votes |
def _acquire(self, wait, wrflag, lockflag): wrflag |= os.O_CREAT fileno = os.open(self.filename, wrflag) try: if not wait: lockflag |= self._module.LOCK_NB self._module.flock(fileno, lockflag) except IOError: os.close(fileno) if not wait: # this is typically # "[Errno 35] Resource temporarily unavailable", # because of LOCK_NB return False else: raise else: self._filedescriptor.fileno = fileno return True
Example #11
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 #12
Source File: flock_tool.py From GYP3 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def ExecFlock(self, lockfile, *cmd_list): """ Emulates the most basic behavior of Linux's flock(1). Rely on exception handling to report errors. Note that the stock python on SunOS has a bug where fcntl.flock(fd, LOCK_EX) always fails with EBADF, that's why we use this F_SETLK hack instead. """ # noinspection PyUnresolvedReferences fd = os.open(lockfile, os.O_WRONLY|os.O_NOCTTY|os.O_CREAT, 0o666) if sys.platform.startswith('aix'): # Python on AIX is compiled with LARGEFILE support, which changes the # struct size. op = struct.pack('hhIllqq', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) else: op = struct.pack('hhllhhl', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) fcntl.fcntl(fd, fcntl.F_SETLK, op) return subprocess.call(cmd_list)
Example #13
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 #14
Source File: gtest_parallel.py From gtest-parallel with Apache License 2.0 | 6 votes |
def __exit__(self, exc_type, exc_value, traceback): # Flush any buffered data to disk. This is needed to prevent race # condition which happens from the moment of releasing file lock # till closing the file. self._fo.flush() try: if sys.platform == 'win32': self._fo.seek(0) msvcrt.locking(self._fo.fileno(), msvcrt.LK_UNLCK, 1) else: fcntl.flock(self._fo.fileno(), fcntl.LOCK_UN) finally: self._fo.close() return exc_value is None
Example #15
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 #16
Source File: filelock.py From veros with MIT License | 5 votes |
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 #17
Source File: gcs_config.py From gokart with MIT License | 5 votes |
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 #18
Source File: mac_tool.py From GYP3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def ExecFlock(self, lockfile, *cmd_list): """Emulates the most basic behavior of Linux's flock(1).""" # Rely on exception handling to report errors. fd = os.open(lockfile, os.O_RDONLY|os.O_NOCTTY|os.O_CREAT, 0o666) fcntl.flock(fd, fcntl.LOCK_EX) return subprocess.call(cmd_list)
Example #19
Source File: lock.py From Jandroid with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _UnlockImplPosix(target_file): fcntl.flock(target_file.fileno(), fcntl.LOCK_UN)
Example #20
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 #21
Source File: filelock.py From veros with MIT License | 5 votes |
def _release(self): # Do not remove the lockfile: # # https://github.com/benediktschmitt/py-filelock/issues/31 # https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition fd = self._lock_file_fd self._lock_file_fd = None fcntl.flock(fd, fcntl.LOCK_UN) os.close(fd) return None # Soft lock # ~~~~~~~~~
Example #22
Source File: utils.py From tvalacarta with GNU General Public License v3.0 | 5 votes |
def _unlock_file(f): fcntl.flock(f, fcntl.LOCK_UN)
Example #23
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 #24
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 #25
Source File: refresher.py From discreETLy with MIT License | 5 votes |
def run(self): with open(LOCK_PATH, 'w') as lock: self.logger.debug("Trying to acquire the S3Stats lock") fcntl.flock(lock, fcntl.LOCK_EX) self.logger.debug("S3Stats Lock acquired") if not self.needs_refresh(): self.logger.info("No need to refresh S3Stats, exiting") return if os.path.exists(STATS_DB_TMP_PATH): os.remove(STATS_DB_TMP_PATH) self.sqlite = sqlite3.connect(STATS_DB_TMP_PATH) self.sqlite.execute(self.CREATE_STMT) self.sqlite.commit() buckets = self.list_buckets() self.logger.info(f"Downloading stats for buckets {buckets}") global_stats = KeyPrefix() for bucket in buckets: data = self.load_s3_bucket(bucket) bucket_stats = self.dump_s3_bucket(data, bucket) global_stats += bucket_stats self.sqlite.execute(self.INSERT_STMT, ("", global_stats.files, global_stats.size_standard, global_stats.size_ia, global_stats.size_glacier, 0, 0)) self.sqlite.commit() self.sqlite.close() os.rename(STATS_DB_TMP_PATH, STATS_DB_PATH)
Example #26
Source File: daemon.py From smarthome with GNU General Public License v3.0 | 5 votes |
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 #27
Source File: file.py From nzb-subliminal with GNU General Public License v3.0 | 5 votes |
def _release(self): try: fileno = self._filedescriptor.fileno except AttributeError: return else: self._module.flock(fileno, self._module.LOCK_UN) os.close(fileno) del self._filedescriptor.fileno
Example #28
Source File: context_managers.py From zulip with Apache License 2.0 | 5 votes |
def lockfile(filename: str, shared: bool=False) -> Iterator[None]: """Lock a file using flock(2) for the duration of a 'with' statement. If shared is True, use a LOCK_SH lock, otherwise LOCK_EX. The file is given by name and will be created if it does not exist.""" with open(filename, 'w') as lock: with flock(lock, shared=shared): yield
Example #29
Source File: context_managers.py From zulip with Apache License 2.0 | 5 votes |
def flock(lockfile: Union[int, IO[Any]], shared: bool=False) -> Iterator[None]: """Lock a file object using flock(2) for the duration of a 'with' statement. If shared is True, use a LOCK_SH lock, otherwise LOCK_EX.""" fcntl.flock(lockfile, fcntl.LOCK_SH if shared else fcntl.LOCK_EX) try: yield finally: fcntl.flock(lockfile, fcntl.LOCK_UN)
Example #30
Source File: dnx_iptables.py From dnxfirewall-cmd with GNU General Public License v3.0 | 5 votes |
def __enter__(self): self._iptables_lock = open(self._iptables_lock_file, 'r+') fcntl.flock(self._iptables_lock, fcntl.LOCK_EX) return self