Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of condition conditional variable in python

Shulou Source: shulou.com Published: 2022-05-31 10:47:26 10月04日 Update

This article mainly explains "what is the role of condition condition variables in python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of condition condition variables in python".

1. The Condition object provided by Python supports complex thread synchronization.

2. Condition is called a conditional variable, which provides wait and notify methods in addition to acquire and release methods similar to Lock. The thread first acquire the condition variable, and then judges some conditions.

Example

Import threading, timeclass Hider (threading.Thread): def _ _ init__ (self, cond, name): super (Hider Self). _ init__ () self.cond = cond self.name = name def run (self): time.sleep (1) # make sure to run the method in Seeker self.cond.acquire () # b print (self.name +': I have been blindfolded') self.cond.notify () self.cond.wait () # c # f print (self.name +': I found you ~ ~') # self.cond.notify () self.cond.release () # g print (self.name +': I won') # hclass Seeker (threading.Thread): def _ _ init__ (self) Cond, name): super (Seeker, self). _ _ init__ () self.cond = cond self.name = name def run (self): self.cond.acquire () self.cond.wait () # a # release trivial occupation At the same time the thread is suspended here until it is notify and repossessed. # d print (self.name +': I have hidden it, please come and find me') self.cond.notify () self.cond.wait () # e # h self.cond.release () print (self.name +': you found it Hey ~ ~') cond = threading.Condition () seeker = Seeker (cond, 'seeker') hider = Hider (cond,' hider') seeker.start () hider.start () Thank you for your reading, the above is the content of "what is the role of condition condition variables in python". After the study of this article, I believe you have a deeper understanding of the role of condition condition variables in python, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Conditions variables functions methods threads learning content complexity at the same time examples objects that is ideas situations articles more eyes knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information MariaDB vpn OPPO Reno