A reader-writer lock for Python that can (must, actually) be used for locking across multiple Python processes.
The rationale and initial implementation of the project can be found in the accompanying blog post.
Installation
This package is available on PyPi, so you can install the latest stable release with a simple pip call:
$ pip install prwlock
Usage
All you have to do is import the module and start using it. There is no need for initialization. Therefore, a code block such as the one below is enough to get an RWLock instance.
from prwlock import RWLock
rwlock = RWLock()
The RWLock itself is pickleable and, therefore, can be passed around to child processes, such as in the code block below.
from __future__ import print_function
import os
import time
from multiprocessing import Pool
from prwlock import RWLock
def f(rwlock):
for i in range(2):
print(os.getpid(), 'Acquiring read lock')
rwlock.acquire_read()
print(os.getpid(), 'Sleeping for a while')
time.sleep(1)
print(os.getpid(), 'Releasing lock')
rwlock.release()
time.sleep(.1)
r = RWLock()
children = 20
pool = Pool(processes=children)
for child in range(children):
pool.apply_async(f, [r])
Context Managers
prwlock also supports context managers using the with syntax. The code block below displays one possible way of using it.
from prwlock import RWLock
# First you instantiate the lock
rwlock = RWLock()
# Now you can lock it in read or in write mode
with rwlock.reader_lock():
# If this executes, then reader lock access has been acquired
print('Reading data')
# Likewise, you can lock in writer mode with:
with rwlock.writer_lock():
print('Writing data')
Contributors
Changes
0.4.1: Fixed bug in which temporary files were left over in $TMPDIR
0.4.0: Added context-management support using the with syntax;
- 0.3.0: Completed the API’s implementation. Namely:
Added support for immediate failure when locks cannot be obtained;
Added timeouts for obtaining the locks.
0.2.0: Added support for RWLocks on Windows XP and above. Changed the API so that the lock can be imported as from prwlock import RWLock, instead of the slightly awkward from prwlock.prwlock import RWLock method.
0.1.1: Fixed the value of the PTHREAD_PROCESS_SHARED constant for Mac OS X. Also added a check to prevent double destruction of the underlying lock on Mac OS X.
0.1.0: Initial release
Metadata
Release files for prwlock 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| prwlock-0.4.1.tar.gz | 12.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| prwlock-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.9 kB
Release files / prwlock-0.4.1.tar.gz
| Download URL | prwlock-0.4.1.tar.gz |
|---|---|
| Size | 12.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a2fa773cb877207ae5b54c7cf5d224b0215c9f7b9ef16a88d33eadc5c9e1466e
|
|
BLAKE2b-256 checksum How to use checksums |
6d16b1a4ef26eb3842b113d4e6e203374ef74827c053c43c438c86ec7dc7a762
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
|
Release files / prwlock-0.4.1-py3-none-any.whl
| Download URL | prwlock-0.4.1-py3-none-any.whl |
|---|---|
| Size | 15.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a12339f729f69985581e68628336446f8abf6c99aadc9fde622ed201024dd37d
|
|
BLAKE2b-256 checksum How to use checksums |
4ad1de4285f150a66ed3fb281560c0d249fe38356730f5ecbaf2847696e3bac1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
|