本文整理匯總了Python中filelock.Timeout方法的典型用法代碼示例。如果您正苦於以下問題:Python filelock.Timeout方法的具體用法?Python filelock.Timeout怎麽用?Python filelock.Timeout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類filelock
的用法示例。
在下文中一共展示了filelock.Timeout方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: save
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def save(self, out, timeout):
filepath = os.path.join(out, self.filename)
lockpath = filepath + '.lock'
try:
with filelock.FileLock(lockpath, timeout=timeout):
saved_assets_list = []
if os.path.isfile(filepath):
with open(filepath) as f:
saved_assets_list = json.load(f)
saved_assets_list.extend(self.cache[self.saved_idx:])
with open(filepath, 'w') as f:
json.dump(saved_assets_list, f, indent=4)
self.saved_idx = len(self.cache)
except filelock.Timeout:
logger.error('Process to write a list of assets is timeout')
示例2: main
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def main(self, *args, **kwargs):
"""Catch all exceptions."""
try:
result = super().main(*args, **kwargs)
return result
except filelock.Timeout:
click.echo((
click.style(
'Unable to acquire lock.\n',
fg='red',
) + 'Hint: Please wait for another renku '
'process to finish and then try again.'
))
except Exception:
if HAS_SENTRY:
self._handle_sentry()
if not (sys.stdin.isatty() and sys.stdout.isatty()):
raise
self._handle_github()
示例3: run
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def run(*args, **kwargs):
"""Main app"""
# define lock
# this prevents multiple, identical circuits from running at the same time
lock = get_lock()
# The main app component initializes the Resilient services
global application
try:
# attempt to lock file, wait 1 second for lock
with lock.acquire(timeout=1):
assert lock.is_locked
application = App(*args, **kwargs)
application.run()
except filelock.Timeout:
# file is probably already locked
print("Failed to acquire lock on {0} - "
"you may have another instance of Resilient Circuits running".format(os.path.abspath(lock.lock_file)))
except OSError as exc:
# Some other problem accessing the lockfile
print("Unable to lock {0}: {1}".format(os.path.abspath(lock.lock_file), exc))
# finally:
# LOG.info("App finished.")
示例4: test_timeout
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def test_timeout(self):
"""
Tests if the lock raises a TimeOut error, when it can not be acquired.
"""
lock1 = self.LOCK_TYPE(self.LOCK_PATH)
lock2 = self.LOCK_TYPE(self.LOCK_PATH)
# Acquire lock 1.
lock1.acquire()
self.assertTrue(lock1.is_locked)
self.assertFalse(lock2.is_locked)
# Try to acquire lock 2.
self.assertRaises(filelock.Timeout, lock2.acquire, timeout=1) # FIXME (Filelock)
self.assertFalse(lock2.is_locked)
self.assertTrue(lock1.is_locked)
# Release lock 1.
lock1.release()
self.assertFalse(lock1.is_locked)
self.assertFalse(lock2.is_locked)
return None
示例5: test_del
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def test_del(self):
"""
Tests, if the lock is released, when the object is deleted.
"""
lock1 = self.LOCK_TYPE(self.LOCK_PATH)
lock2 = self.LOCK_TYPE(self.LOCK_PATH)
# Acquire lock 1.
lock1.acquire()
self.assertTrue(lock1.is_locked)
self.assertFalse(lock2.is_locked)
# Try to acquire lock 2.
self.assertRaises(filelock.Timeout, lock2.acquire, timeout = 1) # FIXME (SoftFileLock)
# Delete lock 1 and try to acquire lock 2 again.
del lock1
lock2.acquire()
self.assertTrue(lock2.is_locked)
lock2.release()
return None
示例6: lock_local_file
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def lock_local_file(path: str) -> filelock.FileLock:
"""Platform dependent file lock.
:param path: File or directory path to lock.
:type path: str
:yield: File lock context manager.
:yield type: :class:`filelock.FileLock`
:raise CloudStorageError: If lock could not be acquired.
"""
lock = filelock.FileLock(path + ".lock")
try:
lock.acquire(timeout=0.1)
except filelock.Timeout:
raise CloudStorageError("Lock timeout")
yield lock
if lock.is_locked:
lock.release()
if os.path.exists(lock.lock_file):
os.remove(lock.lock_file)
示例7: test_lock
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def test_lock(self, local_client):
local_client_2 = local.LocalSyncClient(local_client.path)
local_client.lock(timeout=0.01)
with pytest.raises(filelock.Timeout):
local_client_2.lock(timeout=0.01)
local_client.unlock()
local_client.lock(timeout=0.01)
local_client_2.unlock()
示例8: hold_lock
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def hold_lock(lock_file, reporter=verbosity1):
py.path.local(lock_file.dirname).ensure(dir=1)
lock = FileLock(str(lock_file))
try:
try:
lock.acquire(0.0001)
except Timeout:
reporter("lock file {} present, will block until released".format(lock_file))
lock.acquire()
yield
finally:
lock.release(force=True)
示例9: _config_monitor
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def _config_monitor(config_path):
api_config_path = os.path.join(config_path, 'api')
dirsnapshot = utils.DirSnapshot(api_config_path, CONFIG_FILES_RE)
while True:
try:
with API_CONFIG_LOCK.acquire(timeout=600):
new_snapshot = utils.DirSnapshot(api_config_path, CONFIG_FILES_RE)
if new_snapshot != dirsnapshot:
try:
_load_config(config_path)
_load_auth_dbs(config_path)
except gevent.GreenletExit:
break
except:
LOG.exception('Error loading config')
dirsnapshot = new_snapshot
except filelock.Timeout:
LOG.error('Timeout locking config in config monitor')
gevent.sleep(1)
# initialization
示例10: run
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def run(*args, **kwargs):
"""Main app"""
# define lock
# this prevents multiple, identical circuits from running at the same time
lock = get_lock()
# The main app component initializes the Resilient services
global application
try:
# attempt to lock file, wait 1 second for lock
with lock.acquire(timeout=1):
assert lock.is_locked
application = AppRestartable(*args, **kwargs)
application.run()
except filelock.Timeout:
# file is probably already locked
errmsg = ("Failed to acquire lock on {0} - you may have "
"another instance of Resilient Circuits running")
print(errmsg.format(os.path.abspath(lock.lock_file)))
except ValueError:
LOG.exception("ValueError Raised. Application not running.")
except OSError as exc:
# Some other problem accessing the lockfile
print("Unable to lock {0}: {1}".format(os.path.abspath(lock.lock_file), exc))
# finally:
# LOG.info("App finished.")
示例11: _bootstrap
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def _bootstrap(self, flock_timeout: float = 1.0) -> None:
paths = [
self.storage_backend.PATH_BACKEND(""),
self.storage_backend.PATH_BACKEND("web/simple"),
self.storage_backend.PATH_BACKEND("web/packages"),
self.storage_backend.PATH_BACKEND("web/local-stats/days"),
]
if self.json_save:
logger.debug("Adding json directories to bootstrap")
paths.extend(
[
self.storage_backend.PATH_BACKEND("web/json"),
self.storage_backend.PATH_BACKEND("web/pypi"),
]
)
for path in paths:
path = self.homedir / path
if not path.exists():
logger.info(f"Setting up mirror directory: {path}")
path.mkdir(parents=True)
flock = self.storage_backend.get_lock(str(self.lockfile_path))
try:
logger.debug(f"Acquiring FLock with timeout: {flock_timeout!s}")
with flock.acquire(timeout=flock_timeout):
self._validate_todo()
self._load()
except Timeout:
logger.error("Flock timed out!")
raise RuntimeError(
f"Could not acquire lock on {self.lockfile_path}. "
+ "Another instance could be running?"
)
示例12: instances
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def instances():
makedirs(DATA_DIR)
index_path = os.path.join(DATA_DIR, 'index')
index_lock = os.path.join(DATA_DIR, 'index.lock')
try:
with FileLock(index_lock, timeout=3):
updated = False
if os.path.exists(index_path):
with open(index_path) as fp:
instances = json.loads(uncomment(fp.read()))
# prune unexistent Mechfiles
for k in list(instances):
instance_data = instances[k]
path = instance_data and instance_data.get('path')
if not path or not os.path.exists(os.path.join(path, 'Mechfile')):
del instances[k]
updated = True
else:
instances = {}
if updated:
with open(index_path, 'w') as fp:
json.dump(instances, fp, sort_keys=True, indent=2, separators=(',', ': '))
return instances
except Timeout:
puts_err(colored.red(textwrap.fill("Couldn't access index, it seems locked.")))
sys.exit(1)
示例13: settle_instance
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def settle_instance(instance_name, obj=None, force=False):
makedirs(DATA_DIR)
index_path = os.path.join(DATA_DIR, 'index')
index_lock = os.path.join(DATA_DIR, 'index.lock')
try:
with FileLock(index_lock, timeout=3):
updated = False
if os.path.exists(index_path):
with open(index_path) as fp:
instances = json.loads(uncomment(fp.read()))
# prune unexistent Mechfiles
for k in list(instances):
instance_data = instances[k]
path = instance_data and instance_data.get('path')
if not path or not os.path.exists(os.path.join(path, 'Mechfile')):
del instances[k]
updated = True
else:
instances = {}
instance_data = instances.get(instance_name)
if not instance_data or force:
if obj:
instance_data = instances[instance_name] = obj
updated = True
else:
instance_data = {}
if updated:
with open(index_path, 'w') as fp:
json.dump(instances, fp, sort_keys=True, indent=2, separators=(',', ': '))
return instance_data
except Timeout:
puts_err(colored.red(textwrap.fill("Couldn't access index, it seems locked.")))
sys.exit(1)
示例14: cli_run_with_lock
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def cli_run_with_lock(args=sys.argv[1:]):
cli = parse_args(args)
lock = filelock.FileLock(cli.lock)
log_level = getattr(logging, cli.log_level.upper())
logger.setLevel(log_level)
try:
with lock.acquire(timeout=cli.lock_timeout):
cli.func(cli)
except filelock.Timeout:
logger.info("Another Instance of application already running")
sys.exit(2)
示例15: register_doom_envs_rllib
# 需要導入模塊: import filelock [as 別名]
# 或者: from filelock import Timeout [as 別名]
def register_doom_envs_rllib(**kwargs):
"""Register env factories in RLLib system."""
for spec in DOOM_ENVS:
def make_env_func(env_config):
print('Creating env!!!')
cfg = default_cfg(env=spec.name)
cfg.pixel_format = 'HWC' # tensorflow models expect HWC by default
if 'skip_frames' in env_config:
cfg.env_frameskip = env_config['skip_frames']
if 'res_w' in env_config:
cfg.res_w = env_config['res_w']
if 'res_h' in env_config:
cfg.res_h = env_config['res_h']
if 'wide_aspect_ratio' in env_config:
cfg.wide_aspect_ratio = env_config['wide_aspect_ratio']
env = make_doom_env(spec.name, env_config=env_config, cfg=cfg, **kwargs)
# we lock the global mutex here, otherwise Doom instances may crash on first reset when too many of them are reset simultaneously
lock = FileLock(DOOM_LOCK_PATH)
attempt = 0
while True:
attempt += 1
try:
with lock.acquire(timeout=10):
print('Env created, resetting...')
env.reset()
print('Env reset completed! Config:', env_config)
break
except Timeout:
print('Another instance of this application currently holds the lock, attempt:', attempt)
return env
register_env(spec.name, make_env_func)