From bf4f09be41e8a62664d7925573c85c1481f37883 Mon Sep 17 00:00:00 2001 From: s-razoes <49306008+s-razoes@users.noreply.github.com> Date: Fri, 21 Oct 2022 17:05:18 +0100 Subject: [PATCH 1/3] perfomance change for linux OS --- README.md | 8 +++++++- ilock/__init__.py | 6 +++++- setup.py | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9aeed09..c47eb7d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ilock v.1.0.3 +# ilock v.1.0.4 ## About @@ -62,6 +62,12 @@ with lock: ... ``` +### changelog: +v.1.0.4: +> Minor performance improvement for linux OS: + +> ```when no file location is set, it will use by default the OS memory directory structure``` + ## License diff --git a/ilock/__init__.py b/ilock/__init__.py index 0d223fe..55b2a3e 100644 --- a/ilock/__init__.py +++ b/ilock/__init__.py @@ -7,6 +7,7 @@ import portalocker +MEMORY_FILE_DIRECTORY = '/dev/shm/' class ILockException(Exception): pass @@ -17,7 +18,10 @@ def __init__(self, name, timeout=None, check_interval=0.25, reentrant=False, loc self._timeout = timeout if timeout is not None else 10 ** 8 self._check_interval = check_interval - lock_directory = gettempdir() if lock_directory is None else lock_directory + if lock_directory is None and os.path.isdir(MEMORY_FILE_DIRECTORY): + lock_directory = MEMORY_FILE_DIRECTORY + else: + lock_directory = gettempdir() if lock_directory is None else lock_directory unique_token = sha256(name.encode()).hexdigest() self._filepath = os.path.join(lock_directory, 'ilock-' + unique_token + '.lock') diff --git a/setup.py b/setup.py index ee19c59..9575c18 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -version = '1.0.3' +version = '1.0.4' setup( name='ilock', From 64f8f3dcf178e884fedb793423da63521ca078e5 Mon Sep 17 00:00:00 2001 From: s-razoes <49306008+s-razoes@users.noreply.github.com> Date: Fri, 21 Oct 2022 17:16:45 +0100 Subject: [PATCH 2/3] minor improvement for readability and performance --- ilock/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ilock/__init__.py b/ilock/__init__.py index 55b2a3e..7316031 100644 --- a/ilock/__init__.py +++ b/ilock/__init__.py @@ -7,7 +7,9 @@ import portalocker +#linux memory file directory, for better performance MEMORY_FILE_DIRECTORY = '/dev/shm/' +TEMP_DIR = MEMORY_FILE_DIRECTORY if os.path.isdir(MEMORY_FILE_DIRECTORY) else gettempdir() class ILockException(Exception): pass @@ -18,10 +20,7 @@ def __init__(self, name, timeout=None, check_interval=0.25, reentrant=False, loc self._timeout = timeout if timeout is not None else 10 ** 8 self._check_interval = check_interval - if lock_directory is None and os.path.isdir(MEMORY_FILE_DIRECTORY): - lock_directory = MEMORY_FILE_DIRECTORY - else: - lock_directory = gettempdir() if lock_directory is None else lock_directory + lock_directory = TEMP_DIR if lock_directory is None else lock_directory unique_token = sha256(name.encode()).hexdigest() self._filepath = os.path.join(lock_directory, 'ilock-' + unique_token + '.lock') From b87770aef16334b96ea6ff2118dde4aef6d77cfd Mon Sep 17 00:00:00 2001 From: s-razoes <49306008+s-razoes@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:45:11 +0000 Subject: [PATCH 3/3] Protection against findge case --- ilock/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ilock/__init__.py b/ilock/__init__.py index 7316031..21e67c3 100644 --- a/ilock/__init__.py +++ b/ilock/__init__.py @@ -58,8 +58,12 @@ def __exit__(self, exc_type, exc_val, exc_tb): return if sys.platform.startswith('linux'): - # In Linux you can delete a locked file - os.unlink(self._filepath) + try: + # In Linux you can delete a locked file + os.unlink(self._filepath) + except FileNotFoundError: + # this can happen very rarely + pass self._lockfile.close()