11.python Concurrency Starter (part8 based on thread queue implementation producer consumer model)

Source: Internet
Author: User

First, what is the producer consumer model?

The producer is the thread of the production data, the consumer is the thread that consumes the data.

In multi-threaded development process, producers faster than the speed of consumers, then producers must wait for consumers to finish the data processing, producers will produce new data, relative, if the consumer processing data faster than producers, then consumers must wait for producers.

In order to solve this problem, we have the producer consumer model.


Producer and consumer models, through a container to solve the problem of coupling between producers and consumers, producers and consumers do not directly communicate, so that producers do not have to wait for consumers to finish the data, producers can directly throw data to the queue, this time consumers do not need to find producers to data, Go directly to the queue to fetch the data, this queue, play the role of a buffer, with a balance between producers and consumers of processing capacity.


Ii. example of a queue-based producer consumer model.

Ver1:

#!/usr/local/bin/python2.7

#-*-Coding:utf-8-*-

Import time

Import Random

Import Queue

Import threading

Q1 = Queue.queue ()

def producer (name):

Count = 0

While Count < 10:

Print "Making ..."

Time.sleep (Random.randrange (3))

Q1.put (count)

Print "Procucer%s has produced%s Baozi ..."% (Name,count)

Count + = 1

Print "ok!"

DEF consumer (name):

Count = 0

While Count < 10:

Time.sleep (Random.randrange (4))

If not Q1.empty ():

data = Q1.get ()

Print data

print ' \033[32;1mconsumer%s has eat%s baozi...\033[0m '% (name, data)

Else

Print "Not fond Baozi"

Count + = 1

if __name__ = = ' __main__ ':

P1 = Threading. Thread (target=producer,args= (' A ',))

C1 = Threading. Thread (target=consumer,args= (' B ',))

P1.start ()

C1.start ()


Ver2:

#!/usr/local/bin/python2.7

#-*-Coding:utf-8-*-

Import time

Import Random

Import Queue

Import threading

Q1 = Queue.queue ()

def producer (name):

Count = 0

While Count < 10:

Print "Making ..."

Time.sleep (Random.randrange (3))

Q1.put (count)

Print "Procucer%s has produced%s Baozi ..."% (Name,count)

Count + = 1

Q1.task_done () #给队列发个信号, tell the queue put is complete

#q1. Join ()

Print "ok!"

DEF consumer (name):

Count = 0

While Count < 10:

Q1.join () # #监听生产者发送给队列的信号

Time.sleep (Random.randrange (4))

# if not Q1.empty ():

data = Q1.get ()

#q1. Task_done ()

Print data

print ' \033[32;1mconsumer%s has eat%s baozi...\033[0m '% (name, data)

#else:

#print "Not fond Baozi"

Count + = 1

if __name__ = = ' __main__ ':

P1 = Threading. Thread (target=producer,args= (' A ',))

C1 = Threading. Thread (target=consumer,args= (' B ',))

C2 = Threading. Thread (target=consumer,args= (' C ',))

C3 = Threading. Thread (target=consumer,args= (' D ',))

P1.start ()

C1.start ()

C2.start ()

C3.start ()


This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1925548

11.python Concurrency Starter (part8 based on thread queue implementation producer consumer model)

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.