Specific Condition operation scheme in Python Library

Source: Internet
Author: User

Are you interested in Python? Do you know how to operate Condition in Python Library in practice? If you are interested in the operation steps of the Python Library, you can click on our article to have a better understanding of it.

Condition is a hybrid version of Lock and Event. In addition to being the basic function of Lock, it also provides wait () and Condition y () as the "Message notification" between threads ".

 
 
  1. from threading import *  
  2. from time import *  
  3. condi = Condition()  
  4. def t1():  
  5. condi.acquire()  
  6. try:  
  7. for i in range(10):  
  8. print currentThread().name, i  
  9. sleep(1)  
  10. if (i == 4): condi.wait()    

# Wait () releases the lock and enters the waiting state. The system tries to obtain the lock again after receiving the message sent by notify () and continues subsequent code execution.

 
 
  1. finally:  
  2. condi.release()  
  3. def t2():  
  4. condi.acquire()  
  5. try:  
  6. for i in range(10):  
  7. print currentThread().name, i  
  8. sleep(1)  
  9. finally:  
  10. condi.notify()   

# Notify the waiting thread to get up before releasing the lock.

 
 
  1. condi.release()  
  2. Thread(target=t1).start()  
  3. Thread(target=t2).start()  

Output:

 
 
  1. $ ./main.py  
  2. Thread-1 0  
  3. Thread-1 1  
  4. Thread-1 2  
  5. Thread-1 3   

Thread-1 4 <--- Thread1 release the lock and start waiting. Thread-2 0 <--- Thread2 get the lock and start execution.

 
 
  1. Thread-2 1  
  2. Thread-2 2  
  3. Thread-2 3  
  4. Thread-2 4  
  5. Thread-2 5  
  6. Thread-2 6  
  7. Thread-2 7  
  8. Thread-2 8  

Thread-2 9 <--- Thread2 sends a notification and releases the lock. Thread-1 5 <--- Thread1 receives the message and obtains the lock again to start unfinished work.

 
 
  1. Thread-1 6  
  2. Thread-1 7  
  3. Thread-1 8  
  4. Thread-1 9 

Wait () can actually be divided into two actions: "condi. release ();... acquire. We can use Condition to wrap the existing locks, and of course we can use with/as to improve our code.

 
 
  1. lock = RLock()  
  2. condi = Condition(lock)  
  3. def t1():  
  4. with condi:  
  5. for i in range(10):  
  6. print currentThread().name, i  
  7. sleep(1)  
  8. if (i == 4): condi.wait()  
  9. def t2():  
  10. with lock:  
  11. for i in range(10):  
  12. print currentThread().name, i  
  13. sleep(1)  
  14. condi.notify()  
  15. Thread(target=t1).start()  
  16. Thread(target=t2).start()   

Note that the threads that call Policy () and policyall () must obtain the lock beforehand. Otherwise, an exception is thrown.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.