Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the functions of condition conditional variables in python

Shulou Source: shulou.com Published: 2022-06-03 07:32:10 10月04日 Update

This article mainly explains "what are the functions of condition conditional variables in python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the functions of condition conditional 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 () so far, I believe you have a deeper understanding of "what is the role of condition condition variables in python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Conditions variables methods functions threads content learning complexity practicality deeper interest at the same time examples practicality reality objects simple operation more friends eyes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Apple Xiaomi OPPO Reno