In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you how to use Redis locks to deal with concurrency problems. In order to facilitate you to learn, the detailed code has been placed in the article, interested partners to do it together.
Use Redis locks to deal with concurrency problems to ensure that only one instance of a multi-process is running. When the running instance down, one of the other instances can be topped up to ensure that there is and only one instance is running.
Import redisr = redis.Redis (...) last_heart= 0 # record the last lock heartbeat free_lock_try = 6 # maximum number of lock heartbeats while not r.setnx ('mylock', 1): now_heart= r.get (' mylock') print (f "no lock, now_heart= {now_heart}, last_heart= {last_heart} Free_lock_try= {free_lock_try} ") if now_heart = = last_heart: free_lock_try= free_lock_try-1 if free_lock_try= = 0: # the lock has lost its heartbeat for 1 minute old_heart = r.getset ('mylock', 1) # resets lock to 1 And return the heartbeat value before set if old_heart < now_heart: time.sleep (10) continue else: break # successfully acquire the lock and exit the loop else: free_lock_try = 6 # the lock has a heartbeat Reset free_lock_ try value last_heart = now_heart time.sleep (10) def producer_exit (): "r.delete ('mylock') import atexitatexit.register (producer_exit) # Business Code while True: r.incr (' mylock') # add one to the lock heartbeat when the program exits normally.
Let's take a look at what problems in concurrent locks are solved by this program:
1. Under high concurrency, multiple processes cannot acquire locks at the same time. Redis.setnx is used here, and if the lock already exists, no other process can reset the lock and acquire it. In addition, when multiple processes find that there is no heartbeat at the same time, use redis.getset to reset the heartbeat to 1, all can set successfully, but the value from get is different for multiple processes. Only the process that really acquires the lock returns the heartbeat of the previous process, while other processes get 1.
2. The locked process exits normally. You can use the atexit registration process exit function to delete the lock, or you don't need it here, but the next time you start, you have to wait for the new process to wait for several heartbeats.
3. The locked process exits unexpectedly, and the heartbeat does not increase after the exit. after the number of free_lock_try is exceeded, other processes will reset and acquire the lock.
4. All processes exit unexpectedly. This problem is not a concern of locks. You can use supervisor as a daemon.
On the Redis lock how to deal with concurrency problems to share here, of course, not only the above and everyone's analysis methods, but the editor can ensure its accuracy is absolutely no problem. I hope that the above content can have a certain reference value for everyone, and can be put into practice. If you like this article, you might as well share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.