Skip to main content
100%

A Redis-based distributed lock class, based on the solution proposed by Chris Lamb (https://chris-lamb.co.uk/posts/distributing-locking-python-and-redis).

✓ Published0🌍 Public
Aadewes
Last edited Apr 29, 2014
Created on Jul 29, 2013

This example demonstrates a distributed lock implementation for coordinating access to shared resources in a Python application. It shows a `Lock` class that uses Redis’s watch-multi-exec transaction pipeline to atomically acquire and release locks, with expiration and retry logic to handle contention. The code uses `redis.Redis`, `pipeline()`, `watch()`, `execute()`, and `delete()`, along with `time.sleep` and `time.time` for timeout handling. A command-line test acquires a lock on key `user:abc` for 10 seconds, then releases it after a simulated 20-second wait.

AI-generated description

Similar vizzes