Skip to content

ionelmc/python-redis-lock

Repository files navigation

Overview

docs Documentation Status
tests
GitHub Actions Build Status Requirements Status
Coverage Status Coverage Status
package
PyPI Package latest release PyPI Wheel Supported versions Supported implementations
Commits since latest release

Lock context manager implemented via redis SETNX/BLPOP.

  • Free software: BSD 2-Clause License

Interface targeted to be exactly like threading.Lock.

Usage

Because we don't want to require users to share the lock instance across processes you will have to give them names.

Locks as Context Managers

You can also associate an identifier along with the lock so that it can be retrieved later by the same process, or by a different one. This is useful in cases where the application needs to identify the lock owner (find out who currently owns the lock).

Avoid dogpile effect in django

The dogpile is also known as the thundering herd effect or cache stampede. Here's a pattern to avoid the problem without serving stale data. The work will be performed a single time and every client will wait for the fresh data.

To use this you will need django-redis, however, python-redis-lock provides you a cache backend that has a cache method for your convenience. Just install python-redis-lock like this:

Now put something like this in your settings:

Note

If using a django-redis < 3.8.x, you'll probably need redis_cache which has been deprecated in favor to django_redis. The redis_cache module is removed in django-redis versions > 3.9.x. See django-redis notes.

This backend just adds a convenient .lock(name, expire=None) function to django-redis's cache backend.

You would write your functions like this:

Troubleshooting

In some cases, the lock remains in redis forever (like a server blackout / redis or application crash / an unhandled exception). In such cases, the lock is not removed by restarting the application. One solution is to turn on the auto_renewal parameter in combination with expire to set a time-out on the lock, but let Lock() automatically keep resetting the expire time while your application code is executing:

Another solution is to use the reset_all() function when the application starts:

Alternatively, you can reset individual locks via the reset method.

Use these carefully, if you understand what you do.

Features

  • based on the standard SETNX recipe
  • optional expiry
  • optional timeout
  • optional lock renewal (use a low expire but keep the lock active)
  • no spinloops at acquire

Implementation

redis_lock will use 2 keys for each lock named <name>:

  • lock:<name> - a string value for the actual lock
  • lock-signal:<name> - a list value for signaling the waiters when the lock is released

This is how it works:

python-redis-lock flow diagram

Documentation

https://python-redis-lock.readthedocs.io/en/latest/

Development

To run the all tests run:

tox

Requirements

OS

Any

Runtime

Python 2.7, 3.3 or later, or PyPy

Services

Redis 2.6.12 or later.

Similar projects