11

Except that python-redis-lock module provides contextmanager for the lock object - what are the differences when compared to the lock object you get from redispy module? what is so special about python-redis-lock?

rc = Redis.from_url(settings.BROKER_URL)
lock_str = "bld-%s-lock" % bld_id

Using redispy:

lock = rc.lock(lock_str)

Using python-redis-lock:

lock = redis_lock.Lock(rc, lock_str)
Murali Mopuru
  • 6,086
  • 5
  • 33
  • 51
  • not much difference IMO, you could stick with`redispy`. `redispy.Lock` implemented (`release`) both in pipelines and Lua, with threading support, `python-redis-lock` only in Lua, but has Django integrate. – georgexsh Sep 18 '18 at 08:26

1 Answers1

10

I think the context manager is not the major difference here, because if you see code of redis-py Lock they have the __enter__ and __exit__ added in there.

Both the Lock's seem to use SETNX for acquiring the lock:

The major difference I saw in there was the way blocking the threads work.

  • In case of python-redis-lock they have been using BLPOP mechanism to block the thread, which to me seems like using redis's own version of blocking mechanism. Github code

Something like:

timed_out = not self._client.blpop(self._signal, blpop_timeout) and timeout

  • In case of redis-py they seem use time module and its sleep method to block the thread to check whether the blocking has timedout.

Something like:

import time as mod_time

...
stop_trying_at = mod_time.time() + blocking_timeout
...
mod_time.sleep(sleep)
Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78