A PostgreSQL-backed distributed locking system with heartbeats and automatic stale lock recovery for coordinated resource access.
Project description
Distributed PostgreSQL Lock
A robust distributed locking system using PostgreSQL with automatic heartbeating, timeout management and automatic stale lock recovery for coordinated resource access.
Features
- Distributed Locking: Coordinate resources across multiple processes/machines
- Heartbeating: Automatic keep-alive for long-running operations
- Timeout Handling: Automatic lock expiration
- PostgreSQL Backed: Leverages PostgreSQL's transactional integrity
- Context Managers: Pythonic
withstatement support - Graceful Shutdown: Automatic lock release on process termination
Installation
pip install distributed-pg-lock
Basic Usage
from distributed_pg_lock import DistributedLockManager, db
# Configure database
db.configure(url="postgresql://user:pass@localhost/mydb")
db.create_tables()
# Create lock manager
lock_manager = DistributedLockManager()
# Get a lock
lock = lock_manager.get_lock("critical_resource")
with lock:
if lock.is_acquired:
# Perform critical operations
print("Working with protected resource")
else:
print("Could not acquire lock")
Advanced Usage
# Concurrent processing example
from concurrent.futures import ThreadPoolExecutor
from distributed_pg_lock import DistributedLockManager
lock_manager = DistributedLockManager()
def process_order(order_id):
lock = lock_manager.get_lock(f"order_{order_id}")
with lock:
if lock.is_acquired:
# Process order exclusively
print(f"Processing order {order_id}")
# Process 100 orders concurrently
with ThreadPoolExecutor(max_workers=10) as executor:
for order_id in range(1, 101):
executor.submit(process_order, order_id)
Documentation
Database Configuration
db.configure(
url="postgresql://user:pass@localhost/dbname",
pool_size=10,
max_overflow=20,
echo=False
)
Lock Manager Options
lock_manager = DistributedLockManager(
lock_timeout_minutes=1, # Lock expiration time
owner_id="app-server-1" # Custom owner identifier
)
Force Release Locks (Admin)
lock_manager.force_release_lock("stale_resource")
Testing
pip install -e .[test]
pytest tests/
Performance
See examples/load_test_performance.py for a load testing script that simulates:
- 200 concurrent pods
- Multiple resources
- High contention scenarios
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file distributed_pg_lock-0.1.0.tar.gz.
File metadata
- Download URL: distributed_pg_lock-0.1.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb1d7b152e9aad4c85654d74c30afae91785edf7371e207a0d1af3258958ffa8
|
|
| MD5 |
a801d7ae6c74659ea76aeef9c0cf493a
|
|
| BLAKE2b-256 |
4329c04a866b3e458e24b19f3e8929ffdba9d8f94cc7987b951f29a98be120db
|
File details
Details for the file distributed_pg_lock-0.1.0-py3-none-any.whl.
File metadata
- Download URL: distributed_pg_lock-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b630432d9c6ab806aad0559f709d71aa215e32d78cd02b260338e372566390e
|
|
| MD5 |
a971da9e2623334efd4be3d44591d7e7
|
|
| BLAKE2b-256 |
45c49dfa0b8ae490407b34c3ce38667b490c427b84c854fb70cd48b3b03d9d8a
|