Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the example usage of python thread communication Condition

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the example usage introduction of python thread communication Condition". In the daily operation, I believe that many people have doubts about the example usage introduction of python thread communication Condition. The editor has consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "introduction of example usage of python thread communication Condition". Next, please follow the editor to study!

1. Acquire calls the method associated with Condition.

Acquire () or release () for Lock.

2. Input timeout parameter for wait.

Specifies the maximum number of seconds the thread waits for.

Causes the current thread to enter the waiting pool of the Condition to wait for the notification and release the lock until another thread calls the Condition's notify () or notify_all () method to wake up the thread. When you call the wait () method, you can

3. Notify wakes up a single thread of Condition and notifies it.

The thread that receives the notification automatically calls the accquire () method to try to lock it. If all threads are waiting in the Condition waiting pool, one of the threads is chosen to wake up, and the choice is arbitrary.

4. Notify_all wakes up all threads and notifies them.

Example

Import threadingclass Account: # defines the constructor def _ _ init__ (self, account_no, balance): self.account_no = account_no self._balance = balance self.condition = threading.Condition () # defines the identity self.__deposit_flag = False # def draw (self) that represents whether money has been saved Draw_amount): # locked self.condition.acquire () try: # have not saved yet if not self.__deposit_flag: self.condition.wait () else: if self._balance > = draw_amount: self._balance = self._balance-draw _ amount print (threading.current_thread () .getName () + "withdraw money successfully Account balance is: "+ str (self._balance) +"\ n ") else: print (threading.current_thread (). GetName () +" failed to withdraw money\ n ") # change the logo identifying the deposit already in the account to False self.__deposit_flag = False # to wake up others Wait for the existing thread self.condition.notify_all () finally: # release the lock self.condition.release () # save def deposit (self Deposit_amount): # lock self.condition.acquire () try: # if you have already made a deposit Then waiting for withdrawal if self.__deposit_flag: self.condition.wait () else: self._balance = self._balance + deposit_amount print (threading.current_thread (). GetName () + "deposit successful" The deposit amount is: "+ str (deposit_amount) +"\ n ") # change the deposit ID to deposit self.__deposit_flag = True # wake up other threads self.condition.notify_all () finally: # release lock self.condition.release () def draw_many (account, draw_amount Max): for i in range (max): account.draw (draw_amount) def deposit_many (account, deposit_amount, max): for i in range (max): account.deposit (deposit_amount) # create an account account = Account ("account one", 0) # create and start the money withdrawal thread draw_1 = threading.Thread (name=' withdrawals 1', target=draw_many, args= (account, 200) 50) draw_1.start () draw_2 = threading.Thread (name=' withdrawals II, target=draw_many, args= (account, 200,50) draw_2.start () # create and start the saving thread deposit_1 = threading.Thread (name=' savers 1', target=deposit_many, args= (account, 200,50) deposit_1.start () deposit_2 = threading.Thread (name=' savers II', target=deposit_many, args= (account, 200) 50) deposit_2.start () draw_1.join () draw_2.join () deposit_1.join () deposit_2.join () to this point The study on "introduction to the example usage of python thread communication Condition" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report