This release is a pre-release and may not be stable for production use.
AppLocker - Distributed Application Lock Manager
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
withstatements - Stale lock detection and recovery
Installation
pip install your-package-name
Prerequisites
- Python 3.8+
- Redis server
dgloganddgredispackages (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:
- Redis configuration dictionary (host, port, etc.)
- Your application name (used as part of the lock key)
- Optional parameters:
ttl: Lock time-to-live in seconds (default: 60)logger_: Custom logger instance
Advanced Configuration
You can customize the lock behavior by:
- Setting a specific lock key instead of the default
QUEUE:{application} - Adjusting the stale timeout for force-release operations
- 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
- Keep TTL reasonable - Set it long enough for your operations but not too long (default 60s is good for most cases)
- Always use context managers when possible for safer lock handling
- Check lock ownership before performing sensitive operations
- Monitor lock duration using
get_lock_duration()to optimize your TTL - Handle lock acquisition failures gracefully in your application
Troubleshooting
Common Issues
-
Can't acquire lock
- Check if another instance is running
- Verify Redis connection
- Check if a stale lock needs to be force-released
-
Unexpected lock release
- Ensure your operations complete within the TTL
- Check for network issues with Redis
-
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
Metadata
Release files for dgapplock 2.0.0a3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dgapplock-2.0.0a3.tar.gz | 15.9 kB | Without provenance | Details for dgapplock-2.0.0a3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dgapplock-2.0.0a3-py3-none-any.whl | Python 3 | none | any | Details for dgapplock-2.0.0a3-py3-none-any.whl |
Total release size: 36.5 kB
Release files / dgapplock-2.0.0a3.tar.gz
| Download URL | dgapplock-2.0.0a3.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ecec9908f84cf2e43a6253fd12af63ca592e7ba3eb6f50234895efb65e8bae2
|
|
BLAKE2b-256 checksum How to use checksums |
d6b99295c1c6feac413a4a7cb36dfe93f972316f35664fe3393c40cbb1bcf3f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / dgapplock-2.0.0a3-py3-none-any.whl
| Download URL | dgapplock-2.0.0a3-py3-none-any.whl |
|---|---|
| Size | 20.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
19f41640c34855759745dd12cc957a702fd1326b135b8c9d5c048f388f1679fd
|
|
BLAKE2b-256 checksum How to use checksums |
2a522edee0141c0b1513ead7c5dee60908c16544b78c0800db59b0e71d037fe5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Security for dgapplock 2.0.0a3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.