Python lockfile.LockTimeout() Examples
The following are 6
code examples of lockfile.LockTimeout().
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: Driver.py From happy with Apache License 2.0 | 6 votes |
def lock(self): wait_time = 0.1 wait_attempts = 0 while True: try: if self.filelock.i_am_locking(): self.counter += 1 break self.filelock.acquire(timeout=wait_time) break except lockfile.LockTimeout: pass wait_attempts += 1 if wait_attempts % self.maxattempts == 0: emsg = "Waiting for %s for over %d seconds. Break and Kill itself." % (self.name, wait_time * wait_attempts) self.logger.error("[localhost] Happy: %s" % (emsg)) raise HappyException(emsg) if wait_attempts % 10 == 0: emsg = "Waiting for %s for over %.2f seconds." % (self.name, wait_time * wait_attempts) self.logger.warning("[localhost] Happy: %s" % (emsg))
Example #2
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 #3
Source File: test_action_download.py From st2 with Apache License 2.0 | 6 votes |
def test_run_pack_lock_is_already_acquired(self): action = self.get_action_instance() temp_dir = hashlib.md5(PACK_INDEX['test']['repo_url'].encode()).hexdigest() original_acquire = LockFile.acquire def mock_acquire(self, timeout=None): original_acquire(self, timeout=0.1) LockFile.acquire = mock_acquire try: lock_file = LockFile('/tmp/%s' % (temp_dir)) # Acquire a lock (file) so acquire inside download will fail with open(lock_file.lock_file, 'w') as fp: fp.write('') expected_msg = 'Timeout waiting to acquire lock for' self.assertRaisesRegexp(LockTimeout, expected_msg, action.run, packs=['test'], abs_repo_base=self.repo_base) finally: os.unlink(lock_file.lock_file) LockFile.acquire = original_acquire
Example #4
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 #5
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)
Example #6
Source File: utils.py From flask-restplus-server-example with MIT License | 4 votes |
def download_file( url, local_filepath, chunk_size=1024*512, lock_timeout=10, http_timeout=None, session=None ): # pylint: disable=too-many-arguments """ A helper function which can download a file from a specified ``url`` to a local file ``local_filepath`` in chunks and using a file lock to prevent a concurrent download of the same file. """ # Avoid unnecessary dependencies when the function is not used. import lockfile import requests log.debug("Checking file existance in '%s'", local_filepath) lock = lockfile.LockFile(local_filepath) try: lock.acquire(timeout=lock_timeout) except lockfile.LockTimeout: log.info( "File '%s' is locked. Probably another instance is still downloading it.", local_filepath ) raise try: if not os.path.exists(local_filepath): log.info("Downloading a file from '%s' to '%s'", url, local_filepath) if session is None: session = requests response = session.get(url, stream=True, timeout=http_timeout) if response.status_code != 200: log.error("Download '%s' is failed: %s", url, response) response.raise_for_status() with open(local_filepath, 'wb') as local_file: for chunk in response.iter_content(chunk_size=chunk_size): # filter out keep-alive new chunks if chunk: local_file.write(chunk) log.debug("File '%s' has been downloaded", local_filepath) return local_filepath finally: lock.release()