production-consumer model

import time,random
import queue,threading

q = queue.Queue()

def Producer(name):
    count = 0
    while count < 10:
        print('making-------------')
        time.sleep (random.randrange ( 3 ))
        q.put(count)
        print ( ' Producer %s is producing buns number %s   ' % (name,count))
        count += 1

def Customer(name):

    while True:
        count = 0
        time.sleep(random.randrange(5))
        if not q.empty():
            q.get()
            print ( ' Consumer %s is eating buns number %s ' % (name,q.get()))

        else :
             print ( ' ------------- no more buns ---------------- ' )

        count += 1


p1 = threading.Thread(target=Producer,args='P')
c2 = threading.Thread(target=Customer,args='C')

p1.start()
c2.start()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325616035&siteId=291194637