doraemon the python producer consumer model

Producer consumer model is used to do: to write together separate large function into smaller functional processing

It is to enable the image of two processes:

  • A process producer

  • A process is consumer

  • Container between producers and consumers is the queue

import time
import random
from multiprocess import Process,Queue

def producer(q,name,food):
    for i in range(10):
        time.sleep(random.random())
        fd = '%s%s'%(food,i)
        q.put(fd)
        print('%s生产了一个%s'%(name,food))
        
def consumer(q,name):
    while True:
        food = q.get()
        if not food:break
        the time.sleep (random.radom ( l, 3 ))
         Print ( ' % S eating S% ' % (name, Food))
 # Note: In this case the end consumer does not function 

DEF CP (c_count, P_Count) 
    Q = Queue (10 )
     for I in Range (c_count) 
        Process (target = Consumer, args = (Q, ' liujia ' )) Start (). 
    P_L = []
     for I in Range (P): 
        P1 = Process (target = Producer, = args (Q, ' liudanni ' , '泔水'))
        p1.start()
        p_l.append(p1)
    for p in p_l:p.join()
    for i in range(c_count):
        q.put(None)
        
if __name__ == '__main__':
    cp(2,3)
    

 

Guess you like

Origin www.cnblogs.com/doraemon548542/p/11427314.html