Skip to main content

philiprehberger-lock-run

Tests PyPI version Last updated

Prevent duplicate script execution using file-based locking.

Installation

pip install philiprehberger-lock-run

Usage

Context Manager

from philiprehberger_lock_run import lock

with lock("my-job"):
    do_work()

Decorator

from philiprehberger_lock_run import locked

@locked("my-job")
def scheduled_task():
    ...

Timeout

Wait up to 10 seconds for the lock to become available:

with lock("my-job", timeout=10):
    do_work()

Custom Lock Directory

with lock("my-job", lock_dir="/var/lock"):
    do_work()

Inspecting and cleaning up locks

Check whether another process currently holds a lock (non-blocking):

from philiprehberger_lock_run import is_locked

if is_locked("my-job"):
    print("Another process is running my-job")

Remove orphaned lock files older than a threshold (defaults to 24 hours). Only files that are not currently held are removed:

from philiprehberger_lock_run import cleanup_locks

removed = cleanup_locks(max_age_seconds=3600)
print(f"Removed {len(removed)} stale lock files")

API

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.
is_locked(name, *, lock_dir=None) Return True if another process currently holds the named lock. Performs a non-blocking try-acquire.
cleanup_locks(lock_dir=None, *, max_age_seconds=86400) Remove orphaned lock files older than max_age_seconds that are not currently held. Returns the list of removed file names.
LockError Raised when a lock cannot be acquired. Extends RuntimeError.

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

⭐ Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Metadata

Release files for philiprehberger-lock-run 0.2.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 philiprehberger-lock-run 0.2.0
File Size Uploaded Provenance Actions
philiprehberger_lock_run-0.2.0.tar.gz 183.1 kB Without provenance Details for philiprehberger_lock_run-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-lock-run 0.2.0
File Interpreter ABI Platform Actions
philiprehberger_lock_run-0.2.0-py3-none-any.whl Python 3 none any Details for philiprehberger_lock_run-0.2.0-py3-none-any.whl

Total release size: 188.4 kB

Release files / philiprehberger_lock_run-0.2.0.tar.gz

Download URL philiprehberger_lock_run-0.2.0.tar.gz
Size 183.1 kB
Tags Source
SHA-256 checksum
How to use checksums
83cb7c5fc97a4fc77d22689dc9d0ec45051ff3b97bb09d74817591f911b4202c
BLAKE2b-256 checksum
How to use checksums
61ee149de4530137b2a55caa366b2a7323f46d6484d7267267e0ae4c460a6070
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_lock_run-0.2.0-py3-none-any.whl

Download URL philiprehberger_lock_run-0.2.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ba870bc85b4aabf24fba1f310c8d44670b85bc4e5fe1f1fb939f0dc3d8f4c74
BLAKE2b-256 checksum
How to use checksums
a7092ce68854c30f0474caec3449c46b372eab9e97072e3dc6440c9b2cd4f4ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for philiprehberger-lock-run 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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