Skip to main content

Prevent duplicate script execution using file-based locking

Project description

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

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

philiprehberger_lock_run-0.2.0.tar.gz (183.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_lock_run-0.2.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_lock_run-0.2.0.tar.gz.

File metadata

  • Download URL: philiprehberger_lock_run-0.2.0.tar.gz
  • Upload date:
  • Size: 183.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_lock_run-0.2.0.tar.gz
Algorithm Hash digest
SHA256 83cb7c5fc97a4fc77d22689dc9d0ec45051ff3b97bb09d74817591f911b4202c
MD5 924d2c769d39a80b0caa9e601dc89278
BLAKE2b-256 61ee149de4530137b2a55caa366b2a7323f46d6484d7267267e0ae4c460a6070

See more details on using hashes here.

File details

Details for the file philiprehberger_lock_run-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_lock_run-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ba870bc85b4aabf24fba1f310c8d44670b85bc4e5fe1f1fb939f0dc3d8f4c74
MD5 5459fa4be698a45a04f2ad41c5557b69
BLAKE2b-256 a7092ce68854c30f0474caec3449c46b372eab9e97072e3dc6440c9b2cd4f4ce

See more details on using hashes here.

Supported by

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