0

I haven't used redis in awhile and I'm porting pretty much all of my shared memory item code (aside from queues) in python multiprocessing to using a redis cache, and I needed to re-implement my locks. I found this answer here: https://stackoverflow.com/a/20182448/3467349 - which suggests using brpop as a lock.

But this won't work if it's not possible to pre-instantiate all of the necessary lists. Is there a special way to distinguish nil on a non-existent key and nil?

Community
  • 1
  • 1
user3467349
  • 3,043
  • 4
  • 34
  • 61

1 Answers1

0

There are no empty lists in Redis - once a list has no elements, it is deleted. You can verify that with the EXISTS command.

With BRPOP, however, you don't need to worry about that - even if the key doesn't exist the operation will continue until another client pushes something into that list or the timeout's expiration.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
  • But that's the problem - if the list is empty (but have never been created) then the lock is technically free and yet `bprop` will block - since there is no way to distinguish between between an intentionally emptied list (to signify a `lock` - and one that has never been used). Also I found that the new version of `redis-py` has `lock` call so I'm trying that out. – user3467349 Feb 03 '15 at 21:53
  • In that case you should also look into Salvatore's redlock: http://redis.io/topics/distlock – Itamar Haber Feb 03 '15 at 22:14
  • I got `%timeit x = r.lock('bur'); x.acquire() ; x.release()` - 283 ms, `%timeit r.lpush('bur', 1); r.brpop('bur')` - 182ms. Which doesn't seem to be too different to be concerned. – user3467349 Feb 03 '15 at 22:18