zoukankan      html  css  js  c++  java
  • Python 生产者消费者模型

    一、生产者消费者模型

      1、queue

     1 #!/usr/bin/python
     2 # -*- coding : utf-8 -*-
     3 # 作者: Presley
     4 # 时间: 2018-12-1
     5 # 邮箱:1209989516@qq.com
     6 # 这是我用来练习python 生产者消费者模型的测试脚本
     7 
     8 import threading,queue
     9 
    10 def consume(n):
    11     print("consumer [%s] get task: %s" %(n,q.get()))
    12     q.task_done()
    13 
    14 def producer(n):
    15     for i in range(2):
    16         print("producer [%s] produced a new task: %s" %(n,i))
    17         q.put(i)
    18     #q.join()
    19     print("all taks has been cosumed by consumers...")
    20 
    21 q = queue.Queue()
    22 
    23 c1 = threading.Thread(target=consume,args=[1,])
    24 c2 = threading.Thread(target=consume,args=[2,])
    25 c3 = threading.Thread(target=consume,args=[3,]) #因为只做了两个包子因此第三个消费者会一直阻塞住
    26 
    27 p = threading.Thread(target=producer,args=["wohaoshuai",])
    28 
    29 c1.start()
    30 c2.start()
    31 c3.start()
    32 
    33 p.start()

    执行结果:

    1 C:UserswohaoshuaiAppDataLocalProgramsPythonPython36python.exe E:/PythonLearn/day16/pro_consume.py
    2 producer [wohaoshuai] produced a new task: 0
    3 producer [wohaoshuai] produced a new task: 1
    4 all taks has been cosumed by consumers...
    5 consumer [2] get task: 0
    6 consumer [1] get task: 1

      2、 

        a、q.task_done() ,消费者消费完成后反馈自己已经消费完成

        b、q.join()  生产者监听消费者反馈是否消费完成  

     1 #!/usr/bin/python
     2 # -*- coding : utf-8 -*-
     3 # 作者: Presley
     4 # 时间: 2018-12-1
     5 # 邮箱:1209989516@qq.com
     6 # 这是我用来练习python 生产者消费者模型的测试脚本
     7 
     8 import threading,queue
     9 import time
    10 
    11 def consume(n):
    12     print("consumer [%s] get task: %s" %(n,q.get()))
    13     q.task_done() #每一个消费者吃完自己的包子后就告诉服务员说自己的包子没了
    14 
    15 def producer(n):
    16     count = 1
    17     while True: #循环生产
    18         print("producer [%s] produced a new task: %s" %(n,count))
    19         q.put(count)
    20         q.join() #通知厨师说没包子了,然后等待服务员通知它,当通知到说没包子了就停止等待
    21         print("all taks has been cosumed by consumers...")
    22         count += 1
    23 
    24 q = queue.Queue()
    25 
    26 c1 = threading.Thread(target=consume,args=[1,])
    27 c2 = threading.Thread(target=consume,args=[2,])
    28 c3 = threading.Thread(target=consume,args=[3,]) #因为只做了两个包子因此第三个消费者会一直阻塞住
    29 
    30 p = threading.Thread(target=producer,args=["wohaoshuai",])
    31 
    32 c1.start()
    33 c2.start()
    34 c3.start()
    35 
    36 p.start()

      执行结果:

     1 C:UserswohaoshuaiAppDataLocalProgramsPythonPython36python.exe E:/PythonLearn/day16/pro_consume.py
     2 producer [wohaoshuai] produced a new task: 1
     3 consumer [1] get task: 1
     4 all taks has been cosumed by consumers...
     5 producer [wohaoshuai] produced a new task: 2
     6 consumer [2] get task: 2
     7 all taks has been cosumed by consumers...
     8 producer [wohaoshuai] produced a new task: 3
     9 consumer [3] get task: 3
    10 all taks has been cosumed by consumers...
    11 producer [wohaoshuai] produced a new task: 4

      3、通过判断队列中数据的数量来判断是否生产,防止一次生产过剩而消费不完的情况(此案例存在问题)

     1 #!/usr/bin/python
     2 # -*- coding : utf-8 -*-
     3 # 作者: Presley
     4 # 时间: 2018-12-1
     5 # 邮箱:1209989516@qq.com
     6 # 这是我用来练习python 生产者消费者模型的测试脚本
     7 
     8 import threading,queue
     9 import time
    10 
    11 def consume(n):
    12     while True:
    13         print("consumer [%s] get task: %s" %(n,q.get()))
    14         time.sleep(1)
    15         q.task_done() #每一个消费者吃完自己的包子后就告诉服务员说自己的包子没了
    16 
    17 def producer(n):
    18     count = 1
    19     while True: #循环生产
    20         time.sleep(1)
    21         if q.qsize() < 3:
    22             print("producer [%s] produced a new task: %s" %(n,count))
    23             q.put(count)
    24             q.join() #通知厨师说没包子了,然后等待服务员通知它,当通知到说没包子了就停止等待
    25             print("all taks has been cosumed by consumers...")
    26             count += 1
    27 
    28 
    29 q = queue.Queue()
    30 
    31 c1 = threading.Thread(target=consume,args=[1,])
    32 c2 = threading.Thread(target=consume,args=[2,])
    33 c3 = threading.Thread(target=consume,args=[3,]) #因为只做了两个包子因此第三个消费者会一直阻塞住
    34 
    35 p = threading.Thread(target=producer,args=["wohaoshuai",])
    36 p2 = threading.Thread(target=producer,args=["wohaoshuai2",])
    37 p3 = threading.Thread(target=producer,args=["wohaoshuai3",])
    38 p4 = threading.Thread(target=producer,args=["wohaoshuai4",])
    39 p5 = threading.Thread(target=producer,args=["wohaoshuai5",])
    40 
    41 c1.start()
    42 c2.start()
    43 c3.start()
    44 
    45 p.start()
    46 p2.start()
    47 p3.start()
    48 p4.start()
    49 p5.start()

    执行结果:

     1 C:UserswohaoshuaiAppDataLocalProgramsPythonPython36python.exe E:/PythonLearn/day16/pro_consume.py
     2 producer [wohaoshuai] produced a new task: 1
     3 consumer [1] get task: 1
     4 producer [wohaoshuai5] produced a new task: 1
     5 producer [wohaoshuai3] produced a new task: 1
     6 producer [wohaoshuai4] produced a new task: 1
     7 consumer [2] get task: 1
     8 producer [wohaoshuai2] produced a new task: 1
     9 consumer [3] get task: 1
    10 consumer [1] get task: 1
    11 consumer [3] get task: 1
    12 all taks has been cosumed by consumers...
    13 all taks has been cosumed by consumers...
    14 all taks has been cosumed by consumers...
    15 all taks has been cosumed by consumers...
    16 all taks has been cosumed by consumers...
    17 producer [wohaoshuai] produced a new task: 2
    18 consumer [2] get task: 2
    19 producer [wohaoshuai2] produced a new task: 2
    20 producer [wohaoshuai4] produced a new task: 2
    21 consumer [1] get task: 2
    22 consumer [3] get task: 2
    23 producer [wohaoshuai3] produced a new task: 2
    24 producer [wohaoshuai5] produced a new task: 2
    25 consumer [3] get task: 2
    26 consumer [2] get task: 2
    27 all taks has been cosumed by consumers...
    28 all taks has been cosumed by consumers...
    29 all taks has been cosumed by consumers...
    30 all taks has been cosumed by consumers...
    31 all taks has been cosumed by consumers...
    32 producer [wohaoshuai4] produced a new task: 3
    33 producer [wohaoshuai2] produced a new task: 3
    34 consumer [1] get task: 3
    35 producer [wohaoshuai3] produced a new task: 3
    36 producer [wohaoshuai] produced a new task: 3
    37 producer [wohaoshuai5] produced a new task: 3
    38 consumer [3] get task: 3
    39 consumer [2] get task: 3

       4、修正3中案例

     1 #!/usr/bin/python
     2 # -*- coding : utf-8 -*-
     3 # 作者: Presley
     4 # 时间: 2018-12-1
     5 # 邮箱:1209989516@qq.com
     6 # 这是我用来练习python 生产者消费者模型的测试脚本
     7 
     8 import threading,queue
     9 import time
    10 
    11 def consume(n):
    12     while True:
    13         print("consumer [%s] get task: %s" %(n,q.get()))
    14         time.sleep(1)
    15         q.task_done() #每一个消费者吃完自己的包子后就告诉服务员说自己的包子没了
    16 
    17 def producer(n):
    18     count = 1
    19     while True: #循环生产
    20         print("producer [%s] produced a new task: %s" %(n,count))
    21         q.put(count)
    22         q.join() #判断队列是否为空,等待服务员通知它,当通知到说没包子了就停止挂起
    23         print("all taks has been cosumed by consumers...")
    24         count += 1
    25 
    26 
    27 q = queue.Queue()
    28 
    29 c1 = threading.Thread(target=consume,args=[1,])
    30 c2 = threading.Thread(target=consume,args=[2,])
    31 c3 = threading.Thread(target=consume,args=[3,]) #因为只做了两个包子因此第三个消费者会一直阻塞住
    32 
    33 p = threading.Thread(target=producer,args=["wohaoshuai",])
    34 p2 = threading.Thread(target=producer,args=["wohaoshuai2",])
    35 p3 = threading.Thread(target=producer,args=["wohaoshuai3",])
    36 p4 = threading.Thread(target=producer,args=["wohaoshuai4",])
    37 p5 = threading.Thread(target=producer,args=["wohaoshuai5",])
    38 
    39 c1.start()
    40 c2.start()
    41 c3.start()
    42 
    43 p.start()
    44 p2.start()
    45 p3.start()
    46 p4.start()
    47 p5.start()

    执行结果:

     1 C:UserswohaoshuaiAppDataLocalProgramsPythonPython36python.exe E:/PythonLearn/day16/pro_consume.py
     2 producer [wohaoshuai] produced a new task: 1
     3 consumer [1] get task: 1
     4 producer [wohaoshuai2] produced a new task: 1
     5 consumer [2] get task: 1
     6 producer [wohaoshuai3] produced a new task: 1
     7 consumer [3] get task: 1
     8 producer [wohaoshuai4] produced a new task: 1
     9 producer [wohaoshuai5] produced a new task: 1
    10 consumer [1] get task: 1
    11 consumer [3] get task: 1
    12 all taks has been cosumed by consumers...
    13 all taks has been cosumed by consumers...
    14 producer [wohaoshuai3] produced a new task: 2
    15 consumer [2] get task: 2
    16 producer [wohaoshuai] produced a new task: 2
    17 consumer [1] get task: 2
    18 all taks has been cosumed by consumers...
    19 all taks has been cosumed by consumers...
    20 producer [wohaoshuai2] produced a new task: 2
    21 producer [wohaoshuai4] produced a new task: 2
    22 consumer [3] get task: 2
    23 consumer [2] get task: 2
    24 all taks has been cosumed by consumers...
    25 all taks has been cosumed by consumers...
    26 producer [wohaoshuai] produced a new task: 3
    27 producer [wohaoshuai4] produced a new task: 3
    28 consumer [1] get task: 3
    29 consumer [2] get task: 3
    30 all taks has been cosumed by consumers...
    31 producer [wohaoshuai3] produced a new task: 3
    32 consumer [3] get task: 3
  • 相关阅读:
    [LeetCode] 23. Merge k Sorted Lists ☆☆
    [LeetCode] 22. Generate Parentheses ☆☆
    [LeetCode] 21. Merge Two Sorted Lists ☆
    [LeetCode] 20. Valid Parentheses ☆
    [LeetCode] 19. Remove Nth Node From End of List ☆
    [LeetCode] 18. 4Sum ☆☆
    [LeetCode] 17. Letter Combinations of a Phone Number ☆☆
    [LeetCode] 16. 3Sum Closest ☆☆☆
    [LeetCode] 15. 3Sum ☆☆
    [LeetCode] 14. Longest Common Prefix ☆
  • 原文地址:https://www.cnblogs.com/Presley-lpc/p/10051234.html
Copyright © 2011-2022 走看看