Skip to main content

File-based locks for Python for Linux and Windows

Project description

Locket implements a lock that can be used by multiple processes provided they use the same path.

import locket

# Wait for lock
with locket.lock_file("path/to/lock/file"):
    perform_action()

# Raise error if lock cannot be acquired immediately
with locket.lock_file("path/to/lock/file", timeout=0):
    perform_action()

# Raise error if lock cannot be acquired after thirty seconds
with locket.lock_file("path/to/lock/file", timeout=30):
    perform_action()

# Without context managers:
lock = locket.lock_file("path/to/lock/file")
try:
    lock.acquire()
    perform_action()
finally:
    lock.release()

Locks largely behave as (non-reentrant) Lock instances from the threading module in the standard library. Specifically, their behaviour is:

  • Locks are uniquely identified by the file being locked, both in the same process and across different processes.

  • Locks are either in a locked or unlocked state.

  • When the lock is unlocked, calling acquire() returns immediately and changes the lock state to locked.

  • When the lock is locked, calling acquire() will block until the lock state changes to unlocked, or until the timeout expires.

  • If a process holds a lock, any thread in that process can call release() to change the state to unlocked.

  • Behaviour of locks after fork is undefined.

Installation

pip install locket

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

locket-0.2.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

locket-0.2.1-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file locket-0.2.1.tar.gz.

File metadata

  • Download URL: locket-0.2.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for locket-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3e1faba403619fe201552f083f1ecbf23f550941bc51985ac6ed4d02d25056dd
MD5 b5e4e9dea58264f636f7044c867e2c6d
BLAKE2b-256 0c09c2b9f6ba4fe2aa4e7ca7bb2c2938adcf9731dd22f4ad570ff1188417a080

See more details on using hashes here.

File details

Details for the file locket-0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: locket-0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for locket-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 12b6ada59d1f50710bca9704dbadd3f447dbf8dac6664575c1281cadab8e6449
MD5 66941f09856c304661172b80ee840d71
BLAKE2b-256 50b8e789e45b9b9c2db75e9d9e6ceb022c8d1d7e49b2c085ce8c05600f90a96b

See more details on using hashes here.

Supported by

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