code example of the producer and consumer operations of the Python condition object

Source: Internet
Author: User
This article mainly introduced the Python condition variable's producer and the consumer operation, combined with the concrete instance form to analyze the python condition variable concept, the principle, and the thread operation related skill, the need friend can refer to the next

This paper describes the producer and consumer actions of the Python condition variables. Share to everyone for your reference, as follows:

Mutexes are the simplest thread-synchronization mechanism, and Python provides condition objects in the face of complex thread synchronization problems. condition is called a conditional variable and provides the wait and notify methods in addition to the acquire and release methods similar to lock. The thread first acquire a condition variable and then determines some conditions. Wait if the condition is not met, and if the condition is met, after some processing changes the condition, the other thread is notified by the Notify method, and the other thread in the wait state will be re-judged after receiving the notification. Constantly repeating this process to solve complex synchronization problems.

You can assume that the condition object maintains a lock (Lock/rlock) and a waiting pool. The thread obtains the condition object through acquire, and when the wait method is called, the thread releases the lock inside the condition and enters the blocked state (but does not actually block the current thread) and records the thread in the waiting pool at the same time. When the Notify method is called, the condition object picks a thread from the waiting pool and notifies it to invoke the Acquire method to fetch the lock.

The constructor of the condition object can accept a Lock/rlock object as a parameter, and if not specified, the condition object creates a rlock on its own internally.

Thread Synchronization Classic problem----producer and consumer issues can be easily resolved using conditional variables.

Import Threadingimport Timeclass Producer (threading. Thread): Def init (self): threading. Thread.init (self) def run (self): global count while True:con.acquire () if count <20:count + = 1 print self.name, "Producer product 1,current is%d"% (count) con.notify () Else:print Self.nam E, "Producer say box is full" con.wait () con.release () time.sleep (1) class Consumer (threading. Thread): Def init (self): threading.        Thread.init (self) def run (self): global count while True:con.acquire () if Count>4:count-=4        Print Self.name, "Consumer consume 4,current is%d"% (count) con.notify () else:con.wait () Print Self.name, "Consumer say box is empty" con.release () time.sleep (1) count = 0con = Threading. Condition () def test (): For I in range (1): a = Consumer () A.start () for I in range (1): B =producer () B.start ( If name== ' main ': Test ()

The above code assumes that the consumer is consuming more quickly and the output is:

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.