import queue,threading,time ,random q = queue.Queue() def producer(): count = 1 while count <11: if q.qsize() < 20: s = random.randint(1,100) q.put(s) print('has made baozi ',s) time.sleep(1) count += 1 def consumer(): while True: s = q.get() print('has eaten ',s) time.sleep(1) for i in range(20): t1 = threading.Thread(target=producer) t1.start() for i in range(10): t2 = threading.Thread(target=consumer) t2.start()