14👍
There are two problems:
- As others have said, you need to use two different keys for the lock and the hash.
- The syntax is wrong.
To elaborate on point 2, here are the steps involved with using a lock:
- Create a
Lock
object. - Acquire the lock.
- Do critical stuff.
- Release the lock.
Without using a context manager (the with ...
statement), the code might look like this:
lock = r.lock('my_lock')
lock.acquire(blocking=True)
r.set('foo', 'bar')
lock.release()
With a context manager, that code gets simplified to this:
with r.lock('my_lock'):
r.set('foo', 'bar')
What is happening here is the following:
r.lock()
creates and returns aLock
object.Lock.__enter__()
is called automatically, which in turn callsLock.acquire()
.- The indented code executes.
Lock.__exit__()
is called automatically, which in turn callsLock.release()
.
You can see this in the redis-py source code.
4👍
You are trying to set a dictionary in the same key as the lock. You want to have a key for the lock and another for the dictionary.
- Django backup strategy with dumpdata and migrations
- How to hide a field in django modelform?
- What is the value of self._db by default in Django?
Source:stackexchange.com