Python library for linux file-based read/write locks for inter-process communication
Project description
pylocksfile
pylocksfile is a simple python package for linux-based systems. This package takes advantage of linux's internal file-system locking mechanism and provided a simple read/write locking interface for inter-process communication.
Advantages
pylocksfile has several advantages over the native python locks:
- Allow read/write (shared/exclusive) locking mechanising.
- Significantly smaller over-head for acquire/release operations.
- Supports many different locks within a single pylocksfiles (hence the locks in pylocksfile).
- Interval-based locking mechanising. Supports multiple acquire/release operations simultaneously, as long as locks indices are consecutive.
- Locks are shared via linux file-system, so there is no need to pass the lock object upon process creation, any process may share it at any time.
- Linux provides dead-lock detection, for up to 10 locks dependencies.
Similar libraries
https://github.com/benediktschmitt/py-filelock library "which implements a platform independent file lock in Python".
Usage and Examples
from pylocksfile import pylocksfile
#Create a pylocksfile object. "dataLocksFile.lock" will be the locking file
locksfile = pylocksfile(locksfile_path = "dataLocksFile.lock", verbose = False, l_id = 'process_1')
#Acquiring lock 0 for reading (shared lock) with blocking
locksfile.acquire(writeLock = False, lock_n = 0, blocking = True)
# Do some reading (shared) operation assosiated lock 0.
#Acquiring locks 1,2,3 for writing (exclusive lock) with blocking
locksfile.acquire(writeLock = True, lock_n = (1,3), blocking = True)
# Do some writing (exclusive) operation assosiated locks 1,2,3.
# Do some reading (shared) operation assosiated lock 0.
#Using 'with' statement. Notice (2,1) means offset of 1 locks from lock 2, hence it is equivalent to 'lock_n = 2'.
with locksfile(writeLock = False, lock_n = (2,1)):
#Converts lock 2 to read (shared). Do read (shared) operation on 0,2 and write (exclusive) on 1,3
pass
# Do some writing (exclusive) operation assosiated locks 1,3.
# Do some reading (shared) operation assosiated lock 0.
#Releasing lock 0.
locksfile.release(lock_n = (0,1))
#Releasing all locks from 3 to 1000
locksfile.release(lock_n = (3,997))
#Release all the locks
locksfile.release()
#End of script. On destruction of pylocksfile object, all remaining locks will be freed.
return
Testing
test.py consists of two tests.
- testReadWriteLocks(*args) test is a simple hard-coded script where two processes block each other at each step. It show general correctness of the locking mechanism,
- testRace(*args) tests not only the correctness of the locking mechanism, but also it's speed. The test consists of several process performing hand-over-hand iteration over a cyclic list several times. n_process is the number of processes, n_tracks is the length of the list and n_races is the number of cycles. It show that as n_tracks and n_races increases, pylocksfile is up to 3 times faster than native python locks. As n_process increases, and n_tracks decreases, the speedup decreases to 1 due to context switch over-head.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pylocksfile-0.0.6.tar.gz.
File metadata
- Download URL: pylocksfile-0.0.6.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22d83ee80616ef8cf8445a554f6d7fe8e7b14e08a9631e2e44128fbb0d8756b9
|
|
| MD5 |
083f5e734ce06a071ad202cc0510d0e1
|
|
| BLAKE2b-256 |
4623f566604f65ace29b40b0e86d49fb93406b5bd384a1393ede91c0bf7621d1
|
File details
Details for the file pylocksfile-0.0.6-py3-none-any.whl.
File metadata
- Download URL: pylocksfile-0.0.6-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01c3c9a47b4bdfd079c3a3398c85aacfa4c769710511837e90643f73f2a3cc0b
|
|
| MD5 |
c6b0c4d8f0599b68cdca48958d0c00e3
|
|
| BLAKE2b-256 |
49881ceb3f80a9be39d540ceff552d257d4eb4266df211734a2d5e81ee2db768
|