生产者消费者模型的一个问题
from multiprocessing import Process, Queue, JoinableQueue def consumer(q, name): while 1: info = q.get() print("%s吃了" % name + info) q.task_done() def producer(q, name): for i in range(10): info = '%s生产了%s号包子' % (name, i) q.put(info) q.join() if __name__ == '__main__': q = JoinableQueue(10) pro = Process(target=producer, args=(q, 'Tom',)) con = Process(target=consumer, args=(q, 'Jake')) con.daemon = True pro.start() con.start() pro.join() # from multiprocessing import Process, JoinableQueue # import time # import random # def producer(q, product): # # for i in range(1, 11): # info = '%s %s' % (product, i) # q.put(info) # # # def consumer(q, name): # #time.sleep(0.5) # while 1: # info = q.get() # print('%s消费了%s' % (name, info)) # # # if __name__ == '__main__': # q = JoinableQueue() # pp = Process(target=producer, args=(q, '烤面筋')) # pc1 = Process(target=consumer, args=(q, 'jason')) # pc1.daemon = True # pc1.start() # pp.start() # pp.join()
#之所以会这样,是因为电脑执行太快了,做的快消费的也快,如果做的快消费的慢,那么将不会消费!这与计算机执行快慢有关系。