429154 (2) [Avatar] Offline
#1
My requirement is:

I want to execute a script through which I can set some KEYS, at the same time I can execute a java based application where the same KEY can be reset.
This whole process should be synchronized.

I tried the below script where executing multiple instances, it is acquiring the lock properly
but from java based or using redis-cli still I can access the KEY.

Can I do synchronize KEY from multiple access?

Any help is greatly appreciated.

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'))

- Thanks,
josiah.carlson (77) [Avatar] Offline
#2
Locks in Redis are just semantics over data. They don't actually stop any one or any thing from accessing the data.

The only thing that does stop people from manipulating data in Redis when a lock is held is that *other* clients are checking the lock and not doing stuff when it is held by another process. If you want Java to not access data that is locked from Python, you need to teach your Java code how to use the exact same lock semantics as the Python lock you are already using.
429154 (2) [Avatar] Offline
#3
Okay.
Thanks for the reply.

-Madhu.