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