我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用fcntl.LOCK_UN。
def get_content_all_messages(self): """ Get all messages """ try: buffers = list() with open(self.file_path, 'r') as fd: fcntl.flock(fd, fcntl.LOCK_SH) for row_msg in fd.readlines(): try: msg = self.parse_row_message(row_msg) buffers.append(json.loads(msg.content)) except Exception: pass fcntl.flock(fd, fcntl.LOCK_UN) return buffers except Exception: return
def push(self, message): """ Push a new message. """ if self.overflow_mode == 'drop': if self.max_length > -1 and self.get_length() >= self.max_length: return if self.max_size > -1 and self.get_size() >= self.max_size: return with open(self.file_path, 'a') as fd: # Let's hold an exclusive lock. fcntl.flock(fd, fcntl.LOCK_EX) fd.write(message.serialize()) fcntl.flock(fd, fcntl.LOCK_UN) fd.close() if self.overflow_mode == 'slide': if self.max_size == -1 and self.max_length > -1: while self.get_length() > self.max_length: self.shift() elif self.max_size > -1 and self.max_length == -1: while self.get_size() > self.max_size: self.shift()
def lock_path(path, timeout=0): fd = os.open(path, os.O_CREAT) flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0) flags |= fcntl.FD_CLOEXEC fcntl.fcntl(fd, fcntl.F_SETFD, flags) started = time.time() while True: try: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: if started < time.time() - timeout: raise LockError("Couldn't obtain lock") else: break time.sleep(0.1) def unlock_path(): fcntl.flock(fd, fcntl.LOCK_UN) os.close(fd) return unlock_path
def unlock(self,fname): #TODO: something more clever here? lock_name=fname+'.lock' try: f=self._locks[lock_name] if f is not None: fcntl.lockf(f.fileno(), fcntl.LOCK_UN) f.close() del self._locks[lock_name] # try: # os.unlink(lock_name) # except OSError: #probably somebody else is blocking # pass except KeyError: pass #def __del__(self): #self.do_cleanup() # pass
def release(self): ''' Release a previously acquired lock. ''' assert self._locked assert self._lock_file # Note that this actually leaves the lock file around, but deleting it without a race # is not trivial. fcntl.flock(self._lock_file, fcntl.LOCK_UN) verbose('Lock "%s" released.' % self._lock_file_path) self._locked = False self._lock_file.close() self._lock_file = None
def write_data(self): if self.config_newer_than_data(): self.read_data() return False else: try: self.data = {"clouds":self.quads.clouds.data, "hosts":self.quads.hosts.data, "history":self.quads.history.data, "cloud_history":self.quads.cloud_history.data} with open(self.config, 'w') as yaml_file: fcntl.flock(yaml_file, fcntl.LOCK_EX | fcntl.LOCK_NB) yaml_file.write(yaml.dump(self.data, default_flow_style=False)) fcntl.flock(yaml_file, fcntl.LOCK_UN) self.read_data() return True except Exception, ex: self.logger.error("There was a problem with your file %s" % ex) return False
def config_cache_lock(): """ Obtain the config cache lock, perform the code in the context and then let the lock go. :yield None :return None: """ with open(conf().CONFIG_CACHE_LOCK, 'w+') as lock: try: log.debug( "Getting config cache lock. " "If this blocks forever, try deleting file " "{} and restart the process.".format(conf().CONFIG_CACHE_LOCK) ) flock(lock, LOCK_EX) # Blocks until lock becomes available yield finally: log.debug("Releasing the config cache lock") flock(lock, LOCK_UN)
def __del__(self): import sys import os if not self.initialized: return try: if sys.platform == 'win32': if hasattr(self, 'fd'): os.close(self.fd) os.unlink(self.lockfile) else: import fcntl fcntl.lockf(self.fp, fcntl.LOCK_UN) # os.close(self.fp) if os.path.isfile(self.lockfile): os.unlink(self.lockfile) except Exception as e: raise
def flock(path): """Attempt to acquire a POSIX file lock. """ with open(path, "w+") as lf: try: fcntl.flock(lf, fcntl.LOCK_EX | fcntl.LOCK_NB) acquired = True yield acquired except OSError: acquired = False yield acquired finally: if acquired: fcntl.flock(lf, fcntl.LOCK_UN)
def open_with_lock(filename, mode): with open(filename, mode) as f: while True: try: fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) yield f break except IOError as e: # raise on unrelated IOErrors if e.errno != errno.EAGAIN: raise else: time.sleep(0.1) try: fcntl.flock(f.fileno(), fcntl.LOCK_UN) except Exception: pass
def unlock(self): """Unlocks the LockFile.""" if self._fileobj: # To avoid race conditions with the next caller # waiting for the lock file, it is simply # truncated instead of removed. try: fcntl.lockf(self._fileobj, fcntl.LOCK_UN) self._fileobj.truncate(0) self._fileobj.close() self._lock.release() except EnvironmentError: # If fcntl, or the file operations returned # an exception, drop the lock. Do not catch # the exception that could escape from # releasing the lock. self._lock.release() raise finally: self._fileobj = None else: if self._provide_mutex: assert not self._lock.locked
def updateDB(file_db, msg): db = init_DB(file_db) if not db: data = msg else: namelist = db['??'].split(",") if msg['??'] not in namelist: namelist.append(msg['??']) db['??'] = ",".join(namelist) try: db['eventid'] = db['eventid'] + "," + msg['eventid'] except: pass # if db['??'] != msg['??']: # db['??'] = db['??'] + "," + msg['??'] if "IP" in msg and "IP" in db: if db['IP'] != msg['IP']: db['IP'] = db['IP'] + "," + msg['IP'] db['??'] = db['??'] + msg['??'] data = db with open(file_db, 'w') as f: fcntl.flock(f, fcntl.LOCK_EX) json.dump(data, f, ensure_ascii=False) fcntl.flock(f, fcntl.LOCK_UN)
def display(self, image=None): flock(self.mutex, LOCK_EX) content = self.readRecordingCount() if len(content) == 2 and self.isTakingFrames(content[0]): self.decrementRecordingCount(content[0],self.isStoringWithBg(content[1])) if self.isStoringWithBg(content[1]): self.save(includeBg=True) else: self.save(includeBg=False) if image is None: image = self.buffer self.set_window() pixelbytes = list(Adafruit_ILI9341.image_to_data(image)) self.data(pixelbytes) if self.store and self.isTakingFrames(self.readTouchRecordingCount()[0]): image = Image.new('RGBA',(568, 428)) draw = ImageDraw.Draw(image) draw.ellipse((122+320-self.y, 12+self.x, 152+320-self.y, 42+self.x), fill = 'red', outline ='red') self.save(img=image) self.store = False self.x, self.y = -1, -1 flock(self.mutex, LOCK_UN)
def update(path): """ allow concurrent update of metadata """ p = os.path.join(path, "metadata.json") # we have to open writeable to get a lock with open(p, "a") as f: fcntl.lockf(f, fcntl.LOCK_EX) data = load(path) yield(data) save(path, data) fcntl.lockf(f, fcntl.LOCK_UN)
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')
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()
def main(): exit_status = 1 try: args = parse_args() # Make sure the exporter is only running once. lock_file = '/var/lock/{}.lock'.format(os.path.basename(sys.argv[0])) lock_fd = os.open(lock_file, os.O_CREAT) lock_success = False try: fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) lock_success = True except IOError: msg = 'Failed to export metrics, another instance is running.' syslog.syslog(syslog.LOG_INFO, msg) sys.stderr.write(msg + '\n') if lock_success: # Create a new registry, otherwise unwanted default collectors are # added automatically. registry = prometheus_client.CollectorRegistry() # Register our own collector and write metrics to STDOUT. registry.register(CephRgwCollector(**vars(args))) sys.stdout.write(prometheus_client.generate_latest(registry)) sys.stdout.flush() # Unlock the lock file. fcntl.flock(lock_fd, fcntl.LOCK_UN) exit_status = 0 except Exception as e: syslog.syslog(syslog.LOG_ERR, str(e)) # Cleanup os.close(lock_fd) if lock_success: try: os.unlink(lock_file) except: pass sys.exit(exit_status)
def clean_up(self): # this is not really needed try: if self.fh is not None: if OS_WIN: os.close(self.fh) os.unlink(LOCK_PATH) else: fcntl.lockf(self.fh, fcntl.LOCK_UN) self.fh.close() # ??? os.unlink(LOCK_PATH) except Exception as err: # logger.exception(err) raise # for debugging porpuses, do not raise it on production
def lockfile(file): "flock a given file, then unflock it immediately" if _lockonly(file): flock(file, LOCK_UN) # Options
def __init__(self, mutex_name): check_valid_mutex_name(mutex_name) filename = os.path.join(tempfile.gettempdir(), mutex_name) try: handle = open(filename, 'w') fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) except: self._release_mutex = NULL self._acquired = False try: handle.close() except: pass else: def release_mutex(*args, **kwargs): # Note: can't use self here! if not getattr(release_mutex, 'called', False): release_mutex.called = True try: fcntl.flock(handle, fcntl.LOCK_UN) except: traceback.print_exc() try: handle.close() except: traceback.print_exc() try: # Removing is pretty much optional (but let's do it to keep the # filesystem cleaner). os.unlink(filename) except: pass # Don't use __del__: this approach doesn't have as many pitfalls. self._ref = weakref.ref(self, release_mutex) self._release_mutex = release_mutex self._acquired = True
def close(self): self.save() self.storage.close() if self.lock: fcntl.lockf(self.lock, fcntl.LOCK_UN) self.lock.close() os.unlink(self.lock.name) self.opened = False
def unlock(self): if self.lockfp is None: return fd = self.lockfp.fileno() fcntl.lockf(fd, fcntl.LOCK_UN) self.lockfp.close() self.lockfp = None
def unlock_file(fileobj): fcntl.flock(fileobj, fcntl.LOCK_UN)
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)
def unlock(f): ret = fcntl.flock(_fd(f), fcntl.LOCK_UN) return (ret == 0)
def unlock(self): fcntl.lockf(self.lockfile, fcntl.LOCK_UN)
def __exit__(self, exc_type, exc_value, traceback): if self.fh is not None: fcntl.lockf(self.fh, fcntl.LOCK_UN) self.fh.close() os.unlink(LOCK_PATH)
def _unlock_file(f): fcntl.flock(f, fcntl.LOCK_UN)
def _update(self, mode): if mode is None: if self.locked: fcntl.lockf(self.fd, fcntl.LOCK_UN) self.locked = False elif self.mode is not mode or not self.locked: self.mode = mode self.locked = True for offset in self.locks: fcntl.lockf(self.fd, self.mode, 1, offset)
def _unlock(lockfile): fcntl.lockf(lockfile, fcntl.LOCK_UN)
def unlock(file): fcntl.flock(file.fileno(), fcntl.LOCK_UN)
def release(self): fcntl.flock(self.handle, fcntl.LOCK_UN)
def run(self): """ THIS IS BLOCKING! CALL IT AT LAST! """ with open(Job.lockFileName, 'w') as f: rv = fcntl.lockf(f.fileno(), fcntl.LOCK_EX) print("job {} is running.".format(os.getpid())) f.write(str(os.getpid()) + '\n') f.flush() self.action() fcntl.lockf(f.fileno(), fcntl.LOCK_UN)
def write_server_info(filename, port): pid = os.getpid() rank = MPI.COMM_WORLD.Get_rank() server_info = '{}:{}:{}:{}:{}'.format(LINE_TOKEN, rank, pid, port, LINE_TOKEN).strip() logger.debug("write_server_info: line %s, filename %s", server_info, filename) time.sleep(0.1 * rank) with open(filename, "a") as f: fcntl.lockf(f, fcntl.LOCK_EX) f.write(server_info + '\n') f.flush() os.fsync(f.fileno()) fcntl.lockf(f, fcntl.LOCK_UN) return server_info
def get_rpc_port_by_rank(self, rank, num_servers): if self.mpirun_proc is None: raise RuntimeError("Launch mpirun_proc before reading of rpc ports") if self._rpc_ports is not None: return self._rpc_ports[rank] server_info_pattern = re.compile("^" + LINE_TOKEN + ":([\d]+):([\d]+):([\d]+):" + LINE_TOKEN + "$") self._tmpfile.seek(0) while True: fcntl.lockf(self._tmpfile, fcntl.LOCK_SH) line_count = sum(1 for line in self._tmpfile if server_info_pattern.match(line)) self._tmpfile.seek(0) fcntl.lockf(self._tmpfile, fcntl.LOCK_UN) if line_count == num_servers: break else: time.sleep(0.1) server_infos = [tuple([int(server_info_pattern.match(line).group(1)), int(server_info_pattern.match(line).group(3))]) for line in self._tmpfile] server_infos = sorted(server_infos, key=lambda x: x[0]) self._rpc_ports = [row[1] for row in server_infos] logger.debug("get_rpc_ports: ports (in MPI rank order): %s", self._rpc_ports) self._tmpfile.close() return self._rpc_ports[rank]
def run_chiboard(self): pass import subprocess from chi import board from chi.board import CHIBOARD_HOME, MAGIC_PORT port = None start = False cbc = join(CHIBOARD_HOME, CONFIG_NAME) if os.path.isfile(cbc): with open(cbc) as f: import fcntl try: fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) start = True fcntl.flock(f, fcntl.LOCK_UN) except (BlockingIOError, OSError): # chiboard is running try: data = json.load(f) port = data.get('port') except json.JSONDecodeError: port = None else: start = True if start: from chi.board import main chiboard = main.__file__ subprocess.check_call([sys.executable, chiboard, '--port', str(MAGIC_PORT), '--daemon']) port = MAGIC_PORT if port is None: logger.warning('chiboard seems to be running but port could not be read from its config') else: logger.info(f"{self.f.__name__} started. Check progress at http://localhost:{port}/exp/#/local{self.logdir}")
def release(self): fcntl.flock(self._f, fcntl.LOCK_UN) self._f.close() # def scalar_summaries(prefix='', **kwargs): # vs = [tf.Summary.Value(tag=prefix + '/' + name, simple_value=value) for name, value in kwargs.items()] # s = tf.Summary(value=vs) # return s
def __release(self): self.timer.cancel() self.timer = None fcntl.flock(self.fd, fcntl.LOCK_UN) os.close(self.fd)
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)
def _release_all_locks(self): filedescriptor = self._filedesc if filedescriptor is not None: fcntl.flock(filedescriptor, fcntl.LOCK_UN) os.close(filedescriptor) self._filedescriptor.remove()
def _unlock(self): if not fcntl or not self.ser: return fcntl.flock(self.ser.fileno(), fcntl.LOCK_UN) logging.debug('_unlock')