Python multi-thread programming (7): use Condition for complex synchronization and python multi-thread programming

Source: Internet
Author: User

Python multi-thread programming (7): use Condition for complex synchronization and python multi-thread programming

Currently, we have used Lock to perform mutex access to public resources. We also discussed that the same thread can use RLock to re-import the Lock, however, we only deal with some simple synchronization phenomena in the program, and we cannot even reasonably solve the deadlock problem caused by the use of the Lock. Therefore, we must learn to use a deeper level to solve synchronization problems.

The Condition object provided by Python provides support for complex thread synchronization issues. Condition is called a Condition variable. In addition to providing acquire and release methods similar to Lock, it also provides wait and policy methods.

The main way to use Condition is: the thread first acquire a Condition variable and then judges some conditions. If the condition is not met, wait will. If the condition is met, after some processing changes the condition, other threads will be notified through the notify method, and other threads in the wait status will re-judge the condition after receiving the notification. This process is constantly repeated to solve complicated synchronization problems.

The following uses the famous "producer-consumer" model to demonstrate how to use Condition in Python to implement complex synchronization.
Copy codeThe Code is as follows:
'''
Created on 2012-9-8
 
@ Author: walfred
@ Module: thread. TreadTest7
'''
 
Import threading
Import time
 
Condition = threading. Condition ()
Products = 0
 
Class Producer (threading. Thread ):
Def _ init _ (self ):
Threading. Thread. _ init _ (self)
 
Def run (self ):
Global condition, products
While True:
If condition. acquire ():
If products <10:
Products + = 1;
Print "Producer (% s): deliver one, now products: % s" % (self. name, products)
Condition. Policy ()
Else:
Print "Producer (% s): already 10, stop deliver, now products: % s" % (self. name, products)
Condition. wait ();
Condition. release ()
Time. sleep (2)
 
Class Consumer (threading. Thread ):
Def _ init _ (self ):
Threading. Thread. _ init _ (self)
 
Def run (self ):
Global condition, products
While True:
If condition. acquire ():
If products> 1:
Products-= 1
Print "Consumer (% s): consume one, now products: % s" % (self. name, products)
Condition. Policy ()
Else:
Print "Consumer (% s): only 1, stop consume, products: % s" % (self. name, products)
Condition. wait ();
Condition. release ()
Time. sleep (2)
 
If _ name _ = "_ main __":
For p in range (0, 2 ):
P = Producer ()
P. start ()
 
For c in range (0, 10 ):
C = Consumer ()
C. start ()

The Code mainly implements producer and consumer threads, and both parties will have synchronization issues around products. First, two producers will produce products, and the next 10 Consumers will consume products, run the following code:

Copy codeThe Code is as follows:
Producer (Thread-1): deliver one, now products: 1
Producer (Thread-2): deliver one, now products: 2
Consumer (Thread-3): consume one, now products: 1
Consumer (Thread-4): only 1, stop consume, products: 1
Consumer (Thread-5): only 1, stop consume, products: 1
Consumer (Thread-6): only 1, stop consume, products: 1
Consumer (Thread-7): only 1, stop consume, products: 1
Consumer (Thread-8): only 1, stop consume, products: 1
Consumer (Thread-10): only 1, stop consume, products: 1
Consumer (Thread-9): only 1, stop consume, products: 1
Consumer (Thread-12): only 1, stop consume, products: 1
Consumer (Thread-11): only 1, stop consume, products: 1

In addition, the constructor of the Condition object can take a Lock/RLock object as the parameter. If it is not specified, the Condition object will create an RLock internally. Apart from using y, the Condition object also provides the policyall method to notify all threads in the waiting pool to try the internal acquire lock. Because of the above mechanism, a thread in the waiting state can only be awakened by using the polling y method, so the role of policyall is to prevent a thread from being silenced forever.

Related Article

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.