Python lockfile.FileLock() Examples
The following are 11
code examples of lockfile.FileLock().
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
lockfile
, or try the search function
.
Example #1
Source File: pycronscript.py From whisper-backup with Apache License 2.0 | 6 votes |
def __enter__(self): if self.options.splay > 0: splay = randint(0, self.options.splay) self.logger.debug('Sleeping for %d seconds (splay=%d)' % (splay, self.options.splay)) time.sleep(splay) self.start_time = DT.datetime.today() if not self.options.nolock: self.logger.debug('Attempting to acquire lock %s (timeout %s)', self.options.lockfile, self.options.locktimeout) self.lock = FileLock(self.options.lockfile) try: self.lock.acquire(timeout=self.options.locktimeout) except LockFailed as e: self.logger.error("Lock could not be acquired.") self.logger.error(str(e)) sys.exit(1) except LockTimeout as e: msg = "Lock could not be acquired. Timeout exceeded." self.logger.error(msg) sys.exit(1)
Example #2
Source File: test_state.py From pyexperiment with MIT License | 6 votes |
def test_other_process_locks(self): """Test locking the state in another process locks """ with tempfile.NamedTemporaryFile() as temp: def other_op(queue): """Lock the lockfile, then wait for poison pill """ lockfile.FileLock(temp.name).acquire() while queue.empty(): sleep(0.01) lockfile.FileLock(temp.name).release() queue = multiprocessing.Queue() process = multiprocessing.Process(target=other_op, args=(queue,)) process.start() while not lockfile.FileLock(temp.name).is_locked(): sleep(0.01) StateHandler.STATE_LOCK_TIMEOUT = 0.001 handler = StateHandler(temp.name, load=False) self.assertRaises(RuntimeError, handler.lock) queue.put(None) process.join()
Example #3
Source File: secret_key.py From avos with Apache License 2.0 | 6 votes |
def generate_or_read_from_file(key_file='.secret_key', key_length=64): """Multiprocess-safe secret key file generator. Useful to replace the default (and thus unsafe) SECRET_KEY in settings.py upon first start. Save to use, i.e. when multiple Python interpreters serve the dashboard Django application (e.g. in a mod_wsgi + daemonized environment). Also checks if file permissions are set correctly and throws an exception if not. """ lock = lockfile.FileLock(key_file) with lock: if not os.path.exists(key_file): key = generate_key(key_length) old_umask = os.umask(0o177) # Use '0600' file permissions with open(key_file, 'w') as f: f.write(key) os.umask(old_umask) else: if (os.stat(key_file).st_mode & 0o777) != 0o600: raise FilePermissionError("Insecure key file permissions!") with open(key_file, 'r') as f: key = f.readline() return key
Example #4
Source File: app.py From web2board with GNU Lesser General Public License v3.0 | 5 votes |
def _lock_state_file(self): if not self.lock: return self._lockfile = LockFile(self.path) if (self._lockfile.is_locked() and (time() - getmtime(self._lockfile.lock_file)) > 10): self._lockfile.break_lock() self._lockfile.acquire()
Example #5
Source File: daemon_lock.py From MCVirt with GNU General Public License v2.0 | 5 votes |
def __init__(self, timeout=2): """Create the lock file and lock file object and obtains a lock.""" # Create lock file, if it does not exist if not os.path.isfile(DirectoryLocation.LOCK_FILE): if not os.path.isdir(DirectoryLocation.LOCK_FILE_DIR): os.mkdir(DirectoryLocation.LOCK_FILE_DIR) open(DirectoryLocation.LOCK_FILE, 'a').close() # Attempt to lock lockfile DaemonLock.LOCK = FileLock(DirectoryLocation.LOCK_FILE) # Check if lockfile object is already locked if DaemonLock.LOCK.is_locked(): raise MCVirtLockException('An instance of MCVirt is already running')
Example #6
Source File: Driver.py From happy with Apache License 2.0 | 5 votes |
def __init__(self, name, maxattempts, filename, logger): self.name = name self.filename = filename self.maxattempts = maxattempts self.filelock = lockfile.FileLock(self.filename) self.counter = 0 self.logger = logger
Example #7
Source File: utils.py From Landmark2019-1st-and-3rd-Place-Solution with Apache License 2.0 | 5 votes |
def write_tuning_result(params: dict, result: dict, df_path: str): row = pd.DataFrame() for key in params['tuning_params']: row[key] = [params[key]] for key, val in result.items(): row[key] = val with lockfile.FileLock(df_path): df_results = pd.read_csv(df_path) df_results = pd.concat([df_results, row], sort=False).reset_index(drop=True) df_results.to_csv(df_path, index=None)
Example #8
Source File: download.py From simpleflow with MIT License | 5 votes |
def download(self): self._mkdir_p(self.local_directory) with FileLock(self.lock_location): if not self._check_binary_present(): self._download_binary()
Example #9
Source File: download.py From simpleflow with MIT License | 5 votes |
def _download_binary(self): logger.info("Downloading binary: {} -> {}".format(self.remote_location, self.local_location)) bucket, path = self.remote_location.replace("s3://", "", 1).split("/", 1) # with FileLock(dest): pull(bucket, path, self.local_location) os.chmod(self.local_location, 0o755) # convenience helpers
Example #10
Source File: runIDHelpers.py From bamgineer with Apache License 2.0 | 5 votes |
def IncRunID(project_name, db_dir): """Increment the RunID and append new value with project name to the file""" database_file = db_dir + '/runID_database.txt' # lock the file lock = lf.FileLock(database_file) while not lock.i_am_locking(): try: # wait up to 10 seconds lock.acquire(timeout=5) except lf.LockTimeout: raise Exception( 'ERROR: Timed out waiting for file lock at ' + lock.path) # get the last run_id from the db file rundb = open(database_file, 'r') for line in rundb: (old_id, old_project) = line.split() rundb.close() global run_id run_id = int(old_id) + 1 # write the incremented run_id with project name to the db file with open(database_file, 'a') as rundb: rundb.write(str(run_id) + '\t' + project_name + '\n') rundb.close() lock.release() return
Example #11
Source File: State.py From pyexperiment with MIT License | 5 votes |
def lock(self): """Lock the state file """ self.state_lock = lockfile.FileLock(self.filename) try: self.state_lock.acquire(timeout=self.STATE_LOCK_TIMEOUT) except lockfile.LockTimeout: raise RuntimeError("Cannot acquire lock on state file ('%s'), " "check if another process is using it" % self.filename)