Python multi-thread programming (5): Conditional Variable Synchronization

Source: Internet
Author: User

Mutex lock is the simplest thread synchronization mechanism. The condition object provided by Python provides support for complex thread synchronization problems. 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 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.

It can be considered that the condition object maintains a lock/rlock and a waiting pool. The thread obtains the condition object through acquire. When the wait method is called, the thread releases the internal lock of the condition and enters the blocked state, and records the thread in the waiting pool. When the notify method is called, the condition object selects a thread from the waiting pool and notifies it to call the acquire method to try to obtain the lock.

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.

In addition to the condition y method, the condition object also provides the condition yall 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.

The typical problem of condition variable synchronization is the producer and consumer problem: Assume that a group of producers and a group of consumers interact with the product through a market. The "strategy" of the producer is that if there are less than 1000 remaining products in the market, 100 products will be produced and put on the market; the "policy" of consumers is to consume three products if the number of remaining products in the market is over 100. Use Condition to solve producer and consumer problemsCodeAs follows:

Import Threading
Import Time

Class Producer (threading. Thread ):
Def Run (Self ):
Global Count
While True:
If Con. Acquire ():
If Count> 1000:
Con. Wait ()
Else :
Count = count more than 100
MSG = self. Name + ' Produce 100, Count = ' + STR (count)
Print MSG
Con. Sort y ()
Con. Release ()
Time. Sleep (1)

Class Consumer (threading. Thread ):
Def Run (Self ):
Global Count
While True:
If Con. Acquire ():
If Count <100:
Con. Wait ()
Else :
Count = count-3
MSG = self. Name + ' Consume 3, Count = ' + STR (count)
Print MSG
Con. Sort y ()
Con. Release ()
Time. Sleep (1)

Count = 500
Con = threading. condition ()

Def Test ():
For I In Range (2 ):
P = producer ()
P. Start ()
For I In Range (5 ):
C = Consumer ()
C. Start ()
If _ Name __ =' _ Main __ ' :
Test ()

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.