I want to lock a particular key while it is updating. Tried the below example code.
import redis
import time
conn = redis.StrictRedis(host='localhost', port=6379, db=0)
print ("previous Connected Key value is :" + conn.get('connected'))
print ("previous Operational Key value is :" + conn.get('operational'))
have_lock = False
my_lock = redis.Redis().lock("my_key")
try:
have_lock = my_lock.acquire(blocking=False)
#have_lock = my_lock.acquire(timeout=5)
if have_lock:
print("Got lock. Doing some stuff...")
time.sleep(15)
conn.set('connected', 'false')
conn.set('operational', 'false')
else:
print("Did not acquire lock.")
finally:
if have_lock:
my_lock.release()
print ("After Connected Key value is :" + conn.get('connected'))
print ('After Operational Key value is :' + conn.get('operational'))
The above code is acquiring the lock, but still I can access the KEY from redis-cli or some other application.
How can I lock the KEY?