Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement redis distributed Lock by python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how python implements redis distributed locks. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1 import time 2 import redis 3 from redis.exceptions import WatchError 4 56 class RedisUtil (): 7 def _ init__ (self): 8 self.redis = redis.Redis (host= "redis account url", 9 port=6379,10 password= "your redis password" 11 decode_responses=True) 12 self.online_set_name = "palm_online_devices" 13 14 # acquire lock 15 def acquire_lock (self, key, val, acquire_time=60, time_out=60): 16 start_time = time.time () 17 # blocking wait Polling to acquire lock, not get it, poll wait until lock is acquired or timeout 18 while True:19 if time.time ()-start_time > acquire_time:20 return False21 # use setnx to implement distributed lock. When key exists, setnx does not take any action. If key does not exist, set the value value to val22 # if SETNX returns 1 Indicates that the process acquired the lock, and SETNX sets the value of the key lock.foo to the timeout of the lock (the current time + the valid time of the lock). 23 # if SETNX returns 0, other processes have acquired the lock and the process cannot enter the critical area. The process can keep trying the SETNX operation in a loop to obtain the lock 24 if self.redis.setnx (name=key, value=val): 25 # set the key length to 60 seconds, if more than 60 seconds Key destroys 26 self.redis.expire (key, time_out) 27 return True28 29 # releases lock 30 def release_lock (self, key) Val): 31 pip = self.redis.pipeline (True) 32 while True:33 try:34 pip.watch (key) 35 lock_value = self.redis.get (key) 36 if not lock_value:37 return True38 if lock_value = = val:39 What can pip.multi () 40 pip.delete (key) 41 pip.execute () 42 return True43 pip.unwatch () 44 break45 except WatchError:46 pass47 return Falsepython do Python is a programming language Built-in many effective tools, Python almost omnipotent, the language is easy to understand, easy to start, powerful, in many fields, such as the most popular big data analysis, artificial intelligence, Web development and so on.

These are all the contents of the article "how python implements redis distributed locks". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report