Skip to content

Commit

Permalink
fix create lock function
Browse files Browse the repository at this point in the history
  • Loading branch information
stolarczyk committed Jun 26, 2020
1 parent e436b14 commit 7c28a32
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions ubiquerg/files.py
Expand Up @@ -186,21 +186,21 @@ def create_lock(filepath, wait_max=10):
:param int wait_max: max wait time if the file in question is already locked
"""
lock_path = make_lock_path(filepath)
if os.path.exists(lock_path):
wait_for_lock(lock_path, wait_max)
else:
try:
create_file_racefree(lock_path)
except FileNotFoundError:
os.makedirs(os.path.dirname(filepath))
# wait until no lock is present
wait_for_lock(lock_path, wait_max)
# attempt to lock the file
try:
create_file_racefree(lock_path)
except FileNotFoundError:
parent_dir = os.path.dirname(filepath)
os.makedirs(parent_dir)
create_file_racefree(lock_path)
except Exception as e:
if e.errno == errno.EEXIST:
# Rare case: file already exists;
# the lock has been created in the split second since the last lock existence check,
# wait for the lock file to be gone, but no longer than `wait_max`.
wait_for_lock(lock_path, wait_max)
create_file_racefree(lock_path)
except Exception as e:
if e.errno == errno.EEXIST:
# Rare case: file already exists; the lock has been created in
# the split second since the last lock existence check, wait
# for the lock file to be gone, but no longer than `wait_max`.
warn("Could not create a lock file, it already exists: {}".
format(lock_path))
wait_for_lock(lock_path, wait_max)
else:
raise e
else:
raise e

0 comments on commit 7c28a32

Please sign in to comment.