diff options
author | exprmntr <exprmntr@yandex-team.ru> | 2022-02-10 16:46:50 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:50 +0300 |
commit | a6396ea4e82c2605dcf9a11ebc4c289d0b1c734c (patch) | |
tree | 49e222ea1c5804306084bb3ae065bb702625360f /library/python/filelock | |
parent | 078ddb2fb76387cae14e8c3ceb5a896009a5a539 (diff) | |
download | ydb-a6396ea4e82c2605dcf9a11ebc4c289d0b1c734c.tar.gz |
Restoring authorship annotation for exprmntr <exprmntr@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/python/filelock')
-rw-r--r-- | library/python/filelock/__init__.py | 24 | ||||
-rw-r--r-- | library/python/filelock/ut/lib/test_filelock.py | 76 | ||||
-rw-r--r-- | library/python/filelock/ut/lib/ya.make | 18 | ||||
-rw-r--r-- | library/python/filelock/ut/py2/ya.make | 14 | ||||
-rw-r--r-- | library/python/filelock/ya.make | 16 |
5 files changed, 74 insertions, 74 deletions
diff --git a/library/python/filelock/__init__.py b/library/python/filelock/__init__.py index b68c1d7426..f81ff67f37 100644 --- a/library/python/filelock/__init__.py +++ b/library/python/filelock/__init__.py @@ -3,19 +3,19 @@ import logging import os import sys -import library.python.windows +import library.python.windows logger = logging.getLogger(__name__) -def set_close_on_exec(stream): - if library.python.windows.on_win(): - library.python.windows.set_handle_information(stream, inherit=False) - else: - import fcntl - fcntl.fcntl(stream, fcntl.F_SETFD, fcntl.FD_CLOEXEC) - - +def set_close_on_exec(stream): + if library.python.windows.on_win(): + library.python.windows.set_handle_information(stream, inherit=False) + else: + import fcntl + fcntl.fcntl(stream, fcntl.F_SETFD, fcntl.FD_CLOEXEC) + + class AbstractFileLock(object): def __init__(self, path): @@ -83,12 +83,12 @@ class _WinFileLock(AbstractFileLock): def acquire(self, blocking=True): self._lock = open(self.path) - set_close_on_exec(self._lock) + 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) + locked = library.python.windows.lock_file(self._lock, 0, self._LOCKED_BYTES_NUM, raises=False) if locked: return True if blocking: @@ -98,7 +98,7 @@ class _WinFileLock(AbstractFileLock): def release(self): if self._lock: - library.python.windows.unlock_file(self._lock, 0, self._LOCKED_BYTES_NUM, raises=False) + library.python.windows.unlock_file(self._lock, 0, self._LOCKED_BYTES_NUM, raises=False) self._lock.close() self._lock = None diff --git a/library/python/filelock/ut/lib/test_filelock.py b/library/python/filelock/ut/lib/test_filelock.py index b80e3db8c2..1b11d89123 100644 --- a/library/python/filelock/ut/lib/test_filelock.py +++ b/library/python/filelock/ut/lib/test_filelock.py @@ -2,59 +2,59 @@ import os import time import logging import multiprocessing -import tempfile +import tempfile import threading -import library.python.filelock +import library.python.filelock def _acquire_lock(lock_path, out_file_path): - with library.python.filelock.FileLock(lock_path): + with library.python.filelock.FileLock(lock_path): with open(out_file_path, "a") as out: out.write("{}:{}\n".format(os.getpid(), time.time())) time.sleep(2) def test_filelock(): - temp_dir = tempfile.mkdtemp() - lock_path = os.path.join(temp_dir, "file.lock") - out_file_path = os.path.join(temp_dir, "out.txt") - - process_count = 5 - processes = [] - for i in range(process_count): - process = multiprocessing.Process(target=_acquire_lock, args=(lock_path, out_file_path)) - process.start() - processes.append(process) - - for process in processes: - process.join() - - pids = [] - times = [] - with open(out_file_path) as out: - content = out.read() - logging.info("Times:\n%s", content) - for line in content.strip().split("\n"): - pid, time_val = line.split(":") - pids.append(pid) - times.append(float(time_val)) - - assert len(set(pids)) == process_count - time1 = times.pop() - while times: - time2 = times.pop() - assert int(time1) - int(time2) >= 2 - time1 = time2 + temp_dir = tempfile.mkdtemp() + lock_path = os.path.join(temp_dir, "file.lock") + out_file_path = os.path.join(temp_dir, "out.txt") + + process_count = 5 + processes = [] + for i in range(process_count): + process = multiprocessing.Process(target=_acquire_lock, args=(lock_path, out_file_path)) + process.start() + processes.append(process) + + for process in processes: + process.join() + + pids = [] + times = [] + with open(out_file_path) as out: + content = out.read() + logging.info("Times:\n%s", content) + for line in content.strip().split("\n"): + pid, time_val = line.split(":") + pids.append(pid) + times.append(float(time_val)) + + assert len(set(pids)) == process_count + time1 = times.pop() + while times: + time2 = times.pop() + assert int(time1) - int(time2) >= 2 + time1 = time2 def test_filelock_init_acquired(): - temp_dir = tempfile.mkdtemp() - lock_path = os.path.join(temp_dir, "file.lock") + 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 + with library.python.filelock.FileLock(lock_path): + sublock = library.python.filelock.FileLock(lock_path) + del sublock def test_concurrent_lock(): diff --git a/library/python/filelock/ut/lib/ya.make b/library/python/filelock/ut/lib/ya.make index 3faf072d91..f3f9da5a67 100644 --- a/library/python/filelock/ut/lib/ya.make +++ b/library/python/filelock/ut/lib/ya.make @@ -1,11 +1,11 @@ -OWNER(g:yatool) - +OWNER(g:yatool) + PY23_LIBRARY() - + TEST_SRCS(test_filelock.py) - -PEERDIR( - library/python/filelock -) - -END() + +PEERDIR( + library/python/filelock +) + +END() diff --git a/library/python/filelock/ut/py2/ya.make b/library/python/filelock/ut/py2/ya.make index f0e05f7d21..30b54e0232 100644 --- a/library/python/filelock/ut/py2/ya.make +++ b/library/python/filelock/ut/py2/ya.make @@ -1,9 +1,9 @@ -OWNER(g:yatool) - +OWNER(g:yatool) + PY2TEST() - -PEERDIR( + +PEERDIR( library/python/filelock/ut/lib -) - -END() +) + +END() diff --git a/library/python/filelock/ya.make b/library/python/filelock/ya.make index 87fe650ce0..958cc1866f 100644 --- a/library/python/filelock/ya.make +++ b/library/python/filelock/ya.make @@ -1,11 +1,11 @@ -OWNER(g:yatool) - -PY23_LIBRARY() - -PY_SRCS(__init__.py) - +OWNER(g:yatool) + +PY23_LIBRARY() + +PY_SRCS(__init__.py) + PEERDIR( library/python/windows ) - -END() + +END() |