Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance change for linux based OS #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ilock v.1.0.3
# ilock v.1.0.4


## About
Expand Down Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions ilock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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
Expand All @@ -17,7 +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

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')

Expand Down Expand Up @@ -55,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()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

version = '1.0.3'
version = '1.0.4'

setup(
name='ilock',
Expand Down