In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of recursive locks and semaphore in Mutes locks. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
#!-*-coding:utf-8-*-
# add user mode lock, which is different from global interpreter lock (GIL)
Import threading, time
Def run (n):
Lock.acquire () # acquisition of user-state locks is also called mutex Mutex
Global num # Action
# time.sleep (0.1) # after adding sleep, the program becomes serial. Generally, do not add.
Num + = 1
Lock.release () # release the user state lock
Lock=threading.Lock ()
Num=0
T_objs = []
For i in range (1000):
T = threading.Thread (target=run, args= ("t% s" I,))
T.start ()
T_objs.append (t) # adds each thread instance without blocking the startup of subsequent threads
For t in t_objs: # fetch each thread in the list
T.join () # waiting for each thread in parallel to finish execution and move on
Print ("- all threads has finished...", threading.current_thread (), threading.active_count ())
Print ("num", num)
Threading, time run1 (): print () lock.acquire () num num + = 1 lock.release () num run2 (): print () lock.acquire () num2 num2 + = 1 lock.release () num2 run3 (): lock.acquire () res = run1 () print () res2 = run2 () lock.release () print (res, res2) num, num2 = 0 0 lock = threading.RLock () I range (1): t = threading.Thread (target=run3) t.start () threading.active_count ()! = 1: print (threading.active_count ()): print () print (num, num2) threading Time run (n): semaphore.acquire () time.sleep (1) print (% n) semaphore.release () _ name__==: semaphore=threading.BoundedSemaphore (5) I range (20): t=threading.Thread (target=run,args= (I,)) t.start () threading.active_count ()! = 1:: print () sample analysis of recursive locks and semaphore in Mutes locks is shared here I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can 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.