Using Python to implement producer consumer issues

Source: Internet
Author: User

Previously wrote a producer consumer's implementation in C + +.

Producers and consumers are primarily dealing with mutual exclusion and synchronization issues:

Queue as buffer, requires mutex operation

There are no products in the queue, and consumers need to wait until the producer puts the product in and notifies it. Queues are similar in slow conditions.

Here I use the list to emulate the Python standard library's queue, where I set a size limit of 5:

syncqueue.py

 fromThreadingImportLock fromThreadingImportConditionclassQueue ():def __init__(self): Self.mutex=Lock () self.full=Condition (Self.mutex) Self.empty=Condition (Self.mutex) Self.data= []    defpush (self, Element): Self.mutex.acquire () whileLen (self.data) >= 5: Self.empty.wait () self.data.append (Element) self.full.notify () self. Mutex.release ()defpop (self): Self.mutex.acquire () whileLen (self.data) = =0:self.full.wait () data=Self.data[0] Self.data.pop (0) self.empty.notify () self.mutex.release ()returnDataif __name__=='__main__': Q=Queue () Q.push (10) Q.push (2) Q.push (13)    PrintQ.pop ()PrintQ.pop ()PrintQ.pop ()

This is the most core of the code, note that the inside of the judgment condition to use while loop.

Next is the producer process, producer.py

 fromThreadingImportThread fromRandomImportRandrange fromTimeImportSleep fromSyncqueueImportQueueclassProducerthread (Thread):def __init__(self, queue): Thread.__init__(self) self.queue=QueuedefRun (self): whileTrue:data= Randrange (0, 100) Self.queue.push (data)Print 'Push%d'%(data) sleep (1)if __name__=='__main__': Q=Queue () T=Producerthread (q) T.start () T.join ()

Consumer, condumer.py

 fromThreadingImportThread fromTimeImportSleep fromSyncqueueImportQueueclassConsumerthread (Thread):def __init__(self, queue): Thread.__init__(self) self.queue=QueuedefRun (self): whileTrue:data=Self.queue.pop ()Print 'Pop%d'%(data) sleep (1)if __name__=='__main__': Q=Queue () T=Consumerthread (q) T.start () T.join ()

Finally we write a workshop class that can specify the number of threads:

 fromSyncqueueImportQueue fromProducerImportProducerthread fromConsumerImportConsumerthreadclassWorkShop ():def __init__(self, Producernums, consumernums): Self.producers=[] self.consumers=[] Self.queue=Queue () self.producernums=producernums self.consumernums=consumernumsdefStart (self): forIinchRange (self.producernums): Self.producers.append (Producerthread (self.queue)) forIinchRange (self.consumernums): Self.consumers.append (Consumerthread (self.queue)) forIinchRange (len (self.producers)): Self.producers[i].start () forIinchRange (len (self.consumers)): Self.consumers[i].start () forIinchRange (len (self.producers)): Self.producers[i].join () forIinchRange (len (self.consumers)): Self.consumers[i].join ()if __name__=='__main__': w= WorkShop (3, 4) W.start ()

Finally write a main module:

 from Import WorkShop if __name__ ' __main__ ' :     = WorkShop (2, 3)    W.start ()

Using Python to implement producer consumer issues

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.