Skip to main content

shareable-lock is a lock that can be shared across Python processes.

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shareable_lock-0.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shareable_lock-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file shareable_lock-0.1.0.tar.gz.

File metadata

  • Download URL: shareable_lock-0.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for shareable_lock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fca895eff14ab2f853331268805977327bf34fc28993383e7384efea1d4aaa95
MD5 1529a20bf2957fcf352b0ef4bf1ea877
BLAKE2b-256 4c4923e39a69dd1375af93f57ea92b7caa9a598cf00071968187d07c016b755e

See more details on using hashes here.

File details

Details for the file shareable_lock-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: shareable_lock-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for shareable_lock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef3cf438da64939eff0d8b1f5444dc933097d8fde004f6a0c300a42b9ccb323e
MD5 b77561fffbd295246d4aa73895b6c1be
BLAKE2b-256 348dfde37487c1d8fbc99818e0551dbe2a2b0a5a03afdae34ab9fe084bfba6ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page