{"library":"redlock","title":"Redlock Python Client","description":"redlock is a Python implementation of the Redlock algorithm for distributed locks with Redis. It provides a simple API to acquire and release locks across multiple Redis instances to ensure reliability in distributed systems. The current version is 1.2.0, with a release cadence that is not very frequent, suggesting a stable but less actively developed library.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install redlock"],"cli":null},"imports":["from redlock import Redlock"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import redis\nfrom redlock import Redlock\nimport os\n\n# Configure Redis instances. For production, use multiple instances\n# and ensure they are properly configured for high availability.\n# Here, we default to localhost for a runnable example.\nredis_host = os.environ.get('REDIS_HOST', 'localhost')\nredis_port = int(os.environ.get('REDIS_PORT', 6379))\n\nredis_masters = [\n    {\"host\": redis_host, \"port\": redis_port, \"db\": 0}\n    # For true distributed locking, add more Redis instances:\n    # {\"host\": \"192.168.1.30\", \"port\": 6379, \"db\": 0},\n    # {\"host\": \"192.168.1.31\", \"port\": 6379, \"db\": 0}\n]\n\n# Initialize Redlock with Redis instances\ndlm = Redlock(redis_masters=redis_masters)\n\nresource_name = \"my_critical_resource\"\nlock_ttl = 5000  # Lock validity time in milliseconds (5 seconds)\n\nprint(f\"Attempting to acquire lock for resource: {resource_name}\")\nlock_info = dlm.lock(resource_name, lock_ttl)\n\nif lock_info:\n    print(f\"Successfully acquired lock: {lock_info}\")\n    try:\n        # --- Critical section begins ---\n        print(\"Performing critical operation...\")\n        # Simulate work\n        import time\n        time.sleep(2) # Keep operation less than lock_ttl\n        # --- Critical section ends ---\n    finally:\n        print(\"Releasing lock...\")\n        dlm.unlock(lock_info)\n        print(\"Lock released.\")\nelse:\n    print(f\"Failed to acquire lock for resource: {resource_name}. It might be held by another process.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize Redlock with a single Redis instance (for demonstration) and acquire/release a distributed lock. In a real-world scenario, you should configure multiple independent Redis master instances to leverage the Redlock algorithm's fault tolerance. The `lock_ttl` is specified in milliseconds.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}