diff options
author | Aleksandr <ivansduck@gmail.com> | 2022-02-10 16:47:52 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:52 +0300 |
commit | ea6c5b7f172becca389cacaff7d5f45f6adccbe6 (patch) | |
tree | d16cef493ac1e092b4a03ab9437ec06ffe3d188f /library/python/filelock/__init__.py | |
parent | 37de222addabbef336dcaaea5f7c7645a629fc6d (diff) | |
download | ydb-ea6c5b7f172becca389cacaff7d5f45f6adccbe6.tar.gz |
Restoring authorship annotation for Aleksandr <ivansduck@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/python/filelock/__init__.py')
-rw-r--r-- | library/python/filelock/__init__.py | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/library/python/filelock/__init__.py b/library/python/filelock/__init__.py index f81ff67f37..f62d08b66c 100644 --- a/library/python/filelock/__init__.py +++ b/library/python/filelock/__init__.py @@ -21,7 +21,7 @@ class AbstractFileLock(object): def __init__(self, path): self.path = path - def acquire(self, blocking=True): + def acquire(self, blocking=True): raise NotImplementedError def release(self): @@ -39,24 +39,24 @@ class _NixFileLock(AbstractFileLock): def __init__(self, path): super(_NixFileLock, self).__init__(path) - from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB - self._locker = lambda lock, blocking: flock(lock, LOCK_EX if blocking else LOCK_EX | LOCK_NB) + from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB + self._locker = lambda lock, blocking: flock(lock, LOCK_EX if blocking else LOCK_EX | LOCK_NB) self._unlocker = lambda lock: flock(lock, LOCK_UN) - self._lock = open(self.path, 'a') - set_close_on_exec(self._lock) - - def acquire(self, blocking=True): - import errno - try: - self._locker(self._lock, blocking) - except IOError as e: - if e.errno in (errno.EAGAIN, errno.EACCES) and not blocking: - return False - raise - return True + self._lock = open(self.path, 'a') + set_close_on_exec(self._lock) + + def acquire(self, blocking=True): + import errno + try: + self._locker(self._lock, blocking) + except IOError as e: + if e.errno in (errno.EAGAIN, errno.EACCES) and not blocking: + return False + raise + return True def release(self): - self._unlocker(self._lock) + self._unlocker(self._lock) def __del__(self): if hasattr(self, "_lock"): @@ -81,26 +81,26 @@ class _WinFileLock(AbstractFileLock): if e.errno != errno.EACCES or not os.path.isfile(path): raise - def acquire(self, blocking=True): + def acquire(self, blocking=True): self._lock = open(self.path) set_close_on_exec(self._lock) - + import time locked = False while not locked: locked = library.python.windows.lock_file(self._lock, 0, self._LOCKED_BYTES_NUM, raises=False) - if locked: - return True - if blocking: + if locked: + return True + if blocking: time.sleep(.5) - else: - return False + else: + return False def release(self): if self._lock: library.python.windows.unlock_file(self._lock, 0, self._LOCKED_BYTES_NUM, raises=False) - self._lock.close() - self._lock = None + self._lock.close() + self._lock = None class FileLock(AbstractFileLock): @@ -113,9 +113,9 @@ class FileLock(AbstractFileLock): else: self._lock = _NixFileLock(path) - def acquire(self, blocking=True): - logger.debug('Acquiring filelock (blocking=%s): %s', blocking, self.path) - return self._lock.acquire(blocking) + def acquire(self, blocking=True): + logger.debug('Acquiring filelock (blocking=%s): %s', blocking, self.path) + return self._lock.acquire(blocking) def release(self): logger.debug('Ensuring filelock released: %s', self.path) |