Skip to main content

A PostgreSQL-backed distributed locking system with heartbeats and automatic stale lock recovery for coordinated resource access.

Project description

Distributed PostgreSQL Lock

PyPI version License: MIT

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 with statement 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

distributed_pg_lock-0.1.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

distributed_pg_lock-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

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

Hashes for distributed_pg_lock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eb1d7b152e9aad4c85654d74c30afae91785edf7371e207a0d1af3258958ffa8
MD5 a801d7ae6c74659ea76aeef9c0cf493a
BLAKE2b-256 4329c04a866b3e458e24b19f3e8929ffdba9d8f94cc7987b951f29a98be120db

See more details on using hashes here.

File details

Details for the file distributed_pg_lock-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for distributed_pg_lock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b630432d9c6ab806aad0559f709d71aa215e32d78cd02b260338e372566390e
MD5 a971da9e2623334efd4be3d44591d7e7
BLAKE2b-256 45c49dfa0b8ae490407b34c3ce38667b490c427b84c854fb70cd48b3b03d9d8a

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