• 进程之生产者消费者模型作业


    from multiprocessing import Process,JoinableQueue
    import time
    import random
    
    
    def producer(name,food,q):
        for i in range(3):
            data = '%s生产了%s%s' % (name, food, i)
            time.sleep(random.randint(1, 3))
            print(data)
            q.put(data)
    
    def consumer(name,q):
        while True:
            food = q.get()
            time.sleep(random.randint(1,3))
            print('%s吃了%s' % (name, food))
            q.task_done()
    
    if __name__ == '__main__':
        q = JoinableQueue()
        p1 = Process(target=producer, args=('xxx厨师', '包子', q))
        p2 = Process(target=producer, args=('yyy厨师', '泔水', q))
        c1 = Process(target=consumer, args=('111顾客', q))
        c2 = Process(target=consumer, args=('222顾客', q))
        p1.start()
        p2.start()
        c1.daemon = True
        c2.daemon = True
        c1.start()
        c2.start()
        p1.join()
        p2.join()
        q.join()
  • 相关阅读:
    ACM TJU 1556
    HDU 1890 Robotie Sort
    Android学习笔记
    HDU 2795
    HDU 1542
    HDU 1698
    POJ 2185
    学习笔记
    HDU 3336
    HDU 3746
  • 原文地址:https://www.cnblogs.com/bk134/p/12762421.html
Copyright © 2020-2023  润新知