In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand python semaphores". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
01: semaphore
1) Overview: semaphores are used to control the number of thread concurrency.
2) principle: BoundedSemaphore and Semaphore manage a built-in counter. Whenever resource release increments (call acquire) counter-1, resource consumption decrements (call release) counter + 1.
3) call format: threading.BoundedSemaphore/Semaphore (value); default is 1
4) usage scenarios: parking spaces (fixed parking spaces, all parking spaces are occupied and cannot be entered; only cars can come in without other cars)
5) the difference between BoundedSemaphore and Semaphore: the former will check whether the value of the counter exceeds the initial value of the counter when calling release (), and if so, an exception will be thrown.
6) Note: the counter cannot be less than 0. When the counter is 0, acquire () will block the thread to the synchronous locked state until other threads call release ().
02: there are only 3 parking spaces in the case operation parking lot. When five cars come, they need to stop and use Semaphore to control the number of visitors.
Only three cars can be allowed to enter the parking lot at the same time, and the fourth car can not enter until one of the three cars that enter first comes out.
Import threading,time,random
Semaphore=threading.Semaphore (3) # only 3 threads can be running at a time
Def run (ii):
Semaphore.acquire () # get semaphore: semaphore minus one
Print (car ii,' can enter')
Time.sleep (random.randint (0je 10) * 1)
Print (parking space no. Ii,' released')
Semaphore.release () # release semaphore: semaphore plus one
For i in range (5): # create 5 threads
T=threading.Thread (target=run,args= (I))
T.start ()
Distinguishing case actions between 03:BoundedSemaphore and Semaphore
When BoundedSemaphore is called, an exception is thrown if the value of the counter exceeds the initial value; but Semaphore does not
Import threading,time,random
Semaphore=threading.BoundedSemaphore (3) # only 3 threads can be running at a time
Def run (ii):
Semaphore.acquire () # get semaphore: semaphore minus one
Print (car ii,' can enter')
Time.sleep (random.randint (0je 10) * 1)
Print (parking space no. Ii,' released')
# * High energy here *
Semaphore.release () # release semaphore: semaphore plus one
# release semaphore again: add one semaphore. If the number of semaphores exceeds the limit, an error ValueError: Semaphore released too many times will be reported.
Semaphore.release ()
# * High Energy end *
For i in range (5): # create 5 threads
T=threading.Thread (target=run,args= (I))
This is the end of t.start () "how to understand python semaphores". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.