aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/filelock
diff options
context:
space:
mode:
authorAleksandr <ivansduck@gmail.com>2022-02-10 16:47:52 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:47:52 +0300
commitea6c5b7f172becca389cacaff7d5f45f6adccbe6 (patch)
treed16cef493ac1e092b4a03ab9437ec06ffe3d188f /library/python/filelock
parent37de222addabbef336dcaaea5f7c7645a629fc6d (diff)
downloadydb-ea6c5b7f172becca389cacaff7d5f45f6adccbe6.tar.gz
Restoring authorship annotation for Aleksandr <ivansduck@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/python/filelock')
-rw-r--r--library/python/filelock/__init__.py56
-rw-r--r--library/python/filelock/ut/lib/test_filelock.py56
2 files changed, 56 insertions, 56 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)
diff --git a/library/python/filelock/ut/lib/test_filelock.py b/library/python/filelock/ut/lib/test_filelock.py
index 1b11d89123..a5624f827c 100644
--- a/library/python/filelock/ut/lib/test_filelock.py
+++ b/library/python/filelock/ut/lib/test_filelock.py
@@ -3,7 +3,7 @@ import time
import logging
import multiprocessing
import tempfile
-import threading
+import threading
import library.python.filelock
@@ -48,36 +48,36 @@ def test_filelock():
time1 = time2
-def test_filelock_init_acquired():
+def test_filelock_init_acquired():
temp_dir = tempfile.mkdtemp()
lock_path = os.path.join(temp_dir, "file.lock")
with library.python.filelock.FileLock(lock_path):
sublock = library.python.filelock.FileLock(lock_path)
del sublock
-
-
-def test_concurrent_lock():
- filename = 'con.lock'
-
- def lock():
- l = library.python.filelock.FileLock(filename)
- time.sleep(1)
- l.acquire()
- l.release()
- try:
- os.unlink(filename)
- except OSError:
- pass
-
- threads = []
- for i in range(100):
- t = threading.Thread(target=lock)
- t.daemon = True
- threads.append(t)
-
- for t in threads:
- t.start()
-
- for t in threads:
- t.join()
+
+
+def test_concurrent_lock():
+ filename = 'con.lock'
+
+ def lock():
+ l = library.python.filelock.FileLock(filename)
+ time.sleep(1)
+ l.acquire()
+ l.release()
+ try:
+ os.unlink(filename)
+ except OSError:
+ pass
+
+ threads = []
+ for i in range(100):
+ t = threading.Thread(target=lock)
+ t.daemon = True
+ threads.append(t)
+
+ for t in threads:
+ t.start()
+
+ for t in threads:
+ t.join()