Skip to main content

Shareable Lock

shareable_lock is a lock implementation that uses file locking underneath that can be shared across Python processes. Available locks from multiprocessing and threading don't allow for sharing instances for the same locks across Python processes. With shareable_lock's ShareableLock, a lock for the same resource can be instantiated across different Python processes by creating an instance of the ShareableLock with the same locking file.

Installing

Use uv to install the shareable_lock to your project.

uv pip install shareable_lock

Creating the Lock instance

To create an instance of the lock, you can provide the path for the file associated with the resource being locked. Using the same file name across different processes implies locking the same resource. The create argument should be set to True for one of these processes to create the file used for locking and all other processes can create an instance of the lock for the same resource with create = False.

from shareable_lock import ShareableLock

lock = ShareableLock(fname = "lock.lock", create = True)

Acquiring the Lock

To acquire the lock, create the instance of the ShareableLock and use the acquire method. This uses the fcntl.flock internally to get a fcntl.LOCK_EX type lock on the file. If fcntl.flock fails, an IOError is raised.

from shareable_lock import ShareableLock

lock = ShareableLock()
if lock.acquire(): 
    print("Lock acquired.")

A timeout can also be set to interrupt the acquire method after a give number of seconds pass.

from shareable_lock import ShareableLock

lock = ShareableLock()
if lock.acquire(): 
    print("Lock acquired.")
else:
    print("Acquire method timedout.")

Releasing the Lock

To release the lock, create the instance of the ShareableLock and use the release method. This uses the fcntl.floc(self.fd, fcntl.LOCK_UN) to release the fcntl.LOCK_EX on the file. If fcntl.flock fails, an IOError is raised.

from shareable_lock import ShareableLock

lock = ShareableLock()
lock.acquire()

lock.release()

Trying to release an unacquired lock will raise an assertion error.

from shareable_lock import ShareableLock

lock = ShareableLock()
lock.release()

Deleting the Lock

The lock should be deleted once it is no longer being used by a process.

from shareable_lock import ShareableLock

lock = ShareableLock()
lock.delete()

The process that created the file being used by the ShareableLock should call delete with unlink = True. This will delete the file in addition to closing it.

from shareable_lock import ShareableLock

lock = ShareableLock(create = True)
lock.delete(unlink = True)

Metadata

Release files for shareable-lock 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shareable-lock 0.1.0
File Size Uploaded
shareable_lock-0.1.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shareable-lock 0.1.0
File Interpreter ABI Platform
shareable_lock-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.9 kB

Release files / shareable_lock-0.1.0.tar.gz

Download URL shareable_lock-0.1.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
fca895eff14ab2f853331268805977327bf34fc28993383e7384efea1d4aaa95
BLAKE2b-256 checksum
How to use checksums
4c4923e39a69dd1375af93f57ea92b7caa9a598cf00071968187d07c016b755e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release files / shareable_lock-0.1.0-py3-none-any.whl

Download URL shareable_lock-0.1.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef3cf438da64939eff0d8b1f5444dc933097d8fde004f6a0c300a42b9ccb323e
BLAKE2b-256 checksum
How to use checksums
348dfde37487c1d8fbc99818e0551dbe2a2b0a5a03afdae34ab9fe084bfba6ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page