threading time producer (Threading. Thread): run (self): count True: con.acquire (): count > 1000: con.wait () : count = count+ 100 msg = self.name+ + str (count) &nbSp; msg con.notify () con.release () time.sleep (1) consumer (threading. Thread): run (self): count True: con.acquire (): count < 100: con.wait () &Nbsp; : count = count-3 msg = self.name++str (count) msg Con.notify () Con.release () Time.sleep (1) count = 500con = threading. Condition () test (): i range (2): &Nbsp; p = producer () p.start () i range (5): c = consumer () c.start () == : test ()
Personal Understanding:
The code generates 2 threads for the producer, 5 threads for the consumer
First, producers and consumers are not interfering.
First of all, how producers are produced.
First after executing the first thread such as Count is 600, so release the lock, the next thread starts to run the cumulative +=100, each time the production is finished to inform the consumer, when the producer finds Count 1000, wait
Again consumers have been spending, when the producer of 600, consumer spending finished 597, when Count is the product of 1000, because the producer wait, consumer consumption 3 is 997, then consumer consumption 3 after notify that is to inform the producers
The good thing about doing this
If I am a producer because of a certain factor is not produced, then the consumer is not aware of, at this time the need for producers every production once told the consumer, the same producers also want to know whether consumers in consumption
And if the producer's production rate is greatly larger and the speed of consumption, then wait can be determined by the felling value to limit the maximum length of the queue
This article is from the "Expect batch Sync data" blog, so be sure to keep this source http://4249964.blog.51cto.com/4239964/1573434
Python Threading condition