Prevent duplicate script execution using file-based locking
pip install philiprehberger-lock-runPrevent duplicate script execution using file-based locking.
pip install philiprehberger-lock-run
from philiprehberger_lock_run import lock
with lock("my-job"):
do_work()
from philiprehberger_lock_run import locked
@locked("my-job")
def scheduled_task():
...
Wait up to 10 seconds for the lock to become available:
with lock("my-job", timeout=10):
do_work()
with lock("my-job", lock_dir="/var/lock"):
do_work()
| Name | Description |
|---|---|
lock(name, *, timeout=0, lock_dir=None) | Context manager that acquires a file lock. Raises LockError on failure. |
locked(name, **kwargs) | Decorator that wraps the function body in a file lock. |
LockError | Raised when a lock cannot be acquired. Extends RuntimeError. |
pip install -e .
python -m pytest tests/ -v
If you find this project useful: