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