Skip to main content

AppLocker

Project description

AppLocker - Distributed Application Lock Manager

Python Version Redis Required

Overview

AppLocker is a Python library that provides distributed application locking using Redis. It ensures that only one instance of your application can run a critical section of code at a time, even across multiple servers.

Key features:

  • Distributed locking across multiple application instances
  • Automatic lock renewal (heartbeat) to prevent premature expiration
  • Lock ownership verification to prevent accidental release
  • Context manager support for easy use in with statements
  • Stale lock detection and recovery

Installation

pip install your-package-name

Prerequisites

  • Python 3.8+
  • Redis server
  • dglog and dgredis packages (will be installed automatically if using pip)

Quick Start

from app_locker import AppLocker

# Configure Redis connection
redis_config = {
    "host": "localhost",
    "port": 6379,
    # Add other Redis parameters as needed
}

# Create a locker instance
locker = AppLocker(redis_config, "my_application")

# Usage example
with locker:
    print("This code is protected by a distributed lock")
    # Only one instance of your application can execute this at a time

Configuration

Basic Configuration

When creating an AppLocker instance, you need to provide:

  1. Redis configuration dictionary (host, port, etc.)
  2. Your application name (used as part of the lock key)
  3. Optional parameters:
    • ttl: Lock time-to-live in seconds (default: 60)
    • logger_: Custom logger instance

Advanced Configuration

You can customize the lock behavior by:

  1. Setting a specific lock key instead of the default QUEUE:{application}
  2. Adjusting the stale timeout for force-release operations
  3. Providing a custom logger for tracking lock operations

Usage Examples

Basic Locking

if locker.acquire():
    try:
        # Critical section
        print("Doing important work")
    finally:
        locker.release()
else:
    print("Could not acquire lock - another instance is running")

Context Manager

with locker.acquired():
    # Critical section
    print("This code is protected")

Checking Lock Status

if locker.is_my_lock():
    print("We currently hold the lock")
    
lock_info = locker.get_lock_info()
if lock_info:
    print(f"Lock held by {lock_info['owner']} since {lock_info['acquired_at']}")

Force Release Stale Lock

if locker.force_release_if_stale(stale_timeout=120):
    print("Released a stale lock")

Best Practices

  1. Keep TTL reasonable - Set it long enough for your operations but not too long (default 60s is good for most cases)
  2. Always use context managers when possible for safer lock handling
  3. Check lock ownership before performing sensitive operations
  4. Monitor lock duration using get_lock_duration() to optimize your TTL
  5. Handle lock acquisition failures gracefully in your application

Troubleshooting

Common Issues

  1. Can't acquire lock

    • Check if another instance is running
    • Verify Redis connection
    • Check if a stale lock needs to be force-released
  2. Unexpected lock release

    • Ensure your operations complete within the TTL
    • Check for network issues with Redis
  3. Permission errors

    • Verify Redis credentials in your configuration

Logging

AppLocker provides detailed logging about lock operations. If you're not seeing logs:

  • Make sure logging is configured properly
  • Pass a custom logger to the constructor if needed

API Reference

See the full API documentation for detailed information about all available methods and parameters.

License

MIT License

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

dgapplock-1.0.0a3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

dgapplock-1.0.0a3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file dgapplock-1.0.0a3.tar.gz.

File metadata

  • Download URL: dgapplock-1.0.0a3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for dgapplock-1.0.0a3.tar.gz
Algorithm Hash digest
SHA256 5afaefb1f228c59d87ab4d95da1b021dbc18d23601d9dba64ffba75b2c7a4972
MD5 cec1c4d82292f61b16217e953d95a68b
BLAKE2b-256 6ea73d185a1e2d8067b0c7edc5c99caa70e9d422a67b2d2d9f7f179a7e14c539

See more details on using hashes here.

File details

Details for the file dgapplock-1.0.0a3-py3-none-any.whl.

File metadata

  • Download URL: dgapplock-1.0.0a3-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for dgapplock-1.0.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 6f6738c7c3ccee64a5277440e594395e9d3a7397ceed43acf9c93f2abfc44c3e
MD5 2900b5f23969adfc26e761623643c8bf
BLAKE2b-256 a4c1d33c1e115dbfcddde2d346524ff5c831ef25530dfbfba9221030fae44dec

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