GitHub - mwilliamson/locket.py: File-based locks for Python on Linux and Windows (original) (raw)

locket.py: File-based locks for Python on Linux and Windows

Locket implements a file-based 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 LockError if lock cannot be acquired immediately

with locket.lock_file("path/to/lock/file", timeout=0): perform_action()

Raise LockError 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 threadingmodule in the standard library. Specifically, their behaviour is:

Installation