本文整理汇总了Python中lockfile.LockTimeout方法的典型用法代码示例。如果您正苦于以下问题:Python lockfile.LockTimeout方法的具体用法?Python lockfile.LockTimeout怎么用?Python lockfile.LockTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lockfile
的用法示例。
在下文中一共展示了lockfile.LockTimeout方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lock
# 需要导入模块: import lockfile [as 别名]
# 或者: from lockfile import LockTimeout [as 别名]
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))
示例2: __enter__
# 需要导入模块: import lockfile [as 别名]
# 或者: from lockfile import LockTimeout [as 别名]
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)
示例3: test_run_pack_lock_is_already_acquired
# 需要导入模块: import lockfile [as 别名]
# 或者: from lockfile import LockTimeout [as 别名]
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
示例4: IncRunID
# 需要导入模块: import lockfile [as 别名]
# 或者: from lockfile import LockTimeout [as 别名]
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
示例5: lock
# 需要导入模块: import lockfile [as 别名]
# 或者: from lockfile import LockTimeout [as 别名]
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)
示例6: download_file
# 需要导入模块: import lockfile [as 别名]
# 或者: from lockfile import LockTimeout [as 别名]
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()