A simple directory-based lock implementation for Python. This package provides a lightweight and effective way to coordinate access to shared resources using a lock directory mechanism. This does not require any file locking capabilities of the underlying filesystem or network share.
Install the package via pip:
pip install dirlockYou can use the DirLock class to acquire and release locks in your Python code. The class also supports usage within a with clause for convenience.
import time
from dirlock import DirLock
lock_dir_path = "/tmp/mylockdir.lock"
lock = DirLock(lock_dir_path)
print("Acquire lock...")
lock.acquire()
print("Lock acquired!")
# Perform critical section tasks here
# Simulating work
time.sleep(5)
# Release the lock
lock.release()
print("Lock released.")import time
from dirlock import DirLock
lock_dir_path = "/tmp/mylockdir.lock"
with DirLock(lock_dir_path) as lock:
print("Lock acquired!")
# Perform critical section tasks
time.sleep(5) # Simulate work
print("Work done!")
# The lock is automatically released when the block exits.
print("Lock released.")lock_dir(str): The path to the lock directory.retry_interval(float, default=0.1): Time to wait before retrying if the lock cannot be acquired.timeout_interval(float, default=-1): Timeout for acquiring lock. Set to negative value for no timeout.
You can change the default value for retry_interval
via DirLock.retry_interval=<new_value>.
This package is licensed under the MIT License.