Python Threading condition

Source: Internet
Author: User

 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

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.