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

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
philiprehberger_lock_run-0.2.0.tar.gz 183.1 kB Details

Built distribution (wheel)

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

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

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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