• python-day33--进程间通信(IPC)


    方式:队列(推荐使用)

    一、基本情况

    1.可以往队列里放任意类型的数据

    2. 队列:先进先出

    3.

    q=Queue(3)        #可以设置队列中最多可以进入多少个值,也可以不设置
    q.put('first')
    q.put('second')
    q.put('third')
    # q.put('fourht')    #当设置最多进入3个值时,第四个值过来时就会卡住,只有当第一值     被取出去的时候,第四个值才会进入
    
    print(q.get())
    print(q.get())
    print(q.get())
    # print(q.get())   #当队列中有三个值,而想去取第四个值的时候,也会卡住,只有队列中进入了第四个值的时候才往下运行
    View Code

    4.生产者消费者模型

      1 #生产者消费者模型1
      2 # from multiprocessing import Process,Queue
      3 # import time
      4 # import random
      5 # import os
      6 # def consumer(q):
      7 #     while True:
      8 #         res=q.get()
      9 #         if res is None:
     10 #             break
     11 #         time.sleep(random.randint(1,3))
     12 #         print('33[45m%s 吃了 %s33[0m' % (os.getpid(), res))
     13 # def producer(q):
     14 #     for i in range(5):
     15 #         time.sleep(2)
     16 #         res='包子%s' %i
     17 #         q.put(res)
     18 #         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
     19 #     q.put(None)
     20 # if __name__ == '__main__':
     21 #     q=Queue()
     22 #     #生产者们:厨师们
     23 #     p1=Process(target=producer,args=(q,))
     24 #
     25 #     #消费者们:吃货们
     26 #     p2=Process(target=consumer,args=(q,))
     27 #
     28 #     p1.start()
     29 #     p2.start()
     30 #     p1.join()
     31 #     p2.join()
     32 #     print('主')
     33 
     34 
     35 # #生产者消费者模型2
     36 # from multiprocessing import Process,Queue
     37 # import time
     38 # import random
     39 # import os
     40 # def consumer(q):
     41 #     while True:
     42 #         res=q.get()
     43 #         if res is None:break
     44 #         time.sleep(random.randint(1,3))
     45 #         print('33[45m%s 吃了 %s33[0m' % (os.getpid(), res))
     46 #
     47 # def product_baozi(q):
     48 #     for i in range(3):
     49 #         time.sleep(2)
     50 #         res='包子%s' %i
     51 #         q.put(res)
     52 #         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
     53 #
     54 #
     55 # def product_gutou(q):
     56 #     for i in range(3):
     57 #         time.sleep(2)
     58 #         res='骨头%s' %i
     59 #         q.put(res)
     60 #         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
     61 #
     62 #
     63 # def product_ganshui(q):
     64 #     for i in range(3):
     65 #         time.sleep(2)
     66 #         res='泔水%s' %i
     67 #         q.put(res)
     68 #         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
     69 # if __name__ == '__main__':
     70 #     q=Queue()
     71 #     #生产者们:厨师们
     72 #     p1=Process(target=product_baozi,args=(q,))
     73 #     p2=Process(target=product_gutou,args=(q,))
     74 #     p3=Process(target=product_ganshui,args=(q,))
     75 #
     76 #     #消费者们:吃货们
     77 #     p4=Process(target=consumer,args=(q,))
     78 #     p5=Process(target=consumer,args=(q,))
     79 #
     80 #     # p_l=[p1,p2,p3,p4,p5]
     81 #     # for p in p_l:
     82 #     #     p.start()
     83 #     #
     84 #     # for p in p_l:
     85 #     #     p.join()
     86 #
     87 #
     88 #     p1.start()
     89 #     p2.start()
     90 #     p3.start()
     91 #     p4.start()
     92 #     p5.start()
     93 #
     94 #
     95 #     p1.join()
     96 #     p2.join()
     97 #     p3.join()
     98 #     q.put(None)
     99 #     q.put(None)
    100 #     p4.join()
    101 #     p5.join()
    102 #
    103 #     print('主')
    104 
    105 
    106 
    107 
    108 # #生产者消费者模型3
    109 # from multiprocessing import Process,JoinableQueue
    110 # import time
    111 # import random
    112 # import os
    113 # def consumer(q):
    114 #     while True:
    115 #         res=q.get()
    116 #         time.sleep(random.randint(1,3))
    117 #         print('33[45m%s 吃了 %s33[0m' % (os.getpid(), res))
    118 #         q.task_done()
    119 #
    120 # def product_baozi(q):
    121 #     for i in range(5):
    122 #         time.sleep(2)
    123 #         res='包子%s' %i
    124 #         q.put(res)
    125 #         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
    126 #     q.join()
    127 #
    128 # if __name__ == '__main__':
    129 #     q=JoinableQueue()
    130 #     #生产者们:厨师们
    131 #     p1=Process(target=product_baozi,args=(q,))
    132 #
    133 #     #消费者们:吃货们
    134 #     p4=Process(target=consumer,args=(q,))
    135 #     p4.daemon=True
    136 #
    137 #     p1.start()
    138 #     p4.start()
    139 #
    140 #     p1.join()
    141 #     print('主')
    142 #     #p2结束了
    143 
    144 
    145 
    146 
    147 #生产者消费者模型4
    148 from multiprocessing import Process,JoinableQueue
    149 import time
    150 import random
    151 import os
    152 def consumer(q):
    153     while True:
    154         res=q.get()
    155         time.sleep(random.randint(1,3))
    156         print('33[45m%s 吃了 %s33[0m' % (os.getpid(), res))
    157         q.task_done()
    158 
    159 def product_baozi(q):
    160     for i in range(3):
    161         time.sleep(2)
    162         res='包子%s' %i
    163         q.put(res)
    164         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
    165     q.join()
    166 
    167 def product_gutou(q):
    168     for i in range(3):
    169         time.sleep(2)
    170         res='骨头%s' %i
    171         q.put(res)
    172         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
    173     q.join()
    174 
    175 def product_ganshui(q):
    176     for i in range(3):
    177         time.sleep(2)
    178         res='泔水%s' %i
    179         q.put(res)
    180         print('33[44m%s 制造了 %s33[0m' %(os.getpid(),res))
    181     q.join()
    182 if __name__ == '__main__':
    183     q=JoinableQueue()
    184     #生产者们:厨师们
    185     p1=Process(target=product_baozi,args=(q,))
    186     p2=Process(target=product_gutou,args=(q,))
    187     p3=Process(target=product_ganshui,args=(q,))
    188 
    189     #消费者们:吃货们
    190     p4=Process(target=consumer,args=(q,))
    191     p5=Process(target=consumer,args=(q,))
    192     p4.daemon=True
    193     p5.daemon=True
    194 
    195     p_l=[p1,p2,p3,p4,p5]
    196     for p in p_l:
    197         p.start()
    198 
    199     p1.join()
    200     p2.join()
    201     p3.join()
    202 
    203     print('')
    生产者消费者模型进阶
  • 相关阅读:
    Python使用SMTP模块、email模块发送邮件
    harbor搭建及使用
    ELK搭建-windows
    ELK技术栈之-Logstash详解
    【leetcode】1078. Occurrences After Bigram
    【leetcode】1073. Adding Two Negabinary Numbers
    【leetcode】1071. Greatest Common Divisor of Strings
    【leetcode】449. Serialize and Deserialize BST
    【leetcode】1039. Minimum Score Triangulation of Polygon
    【leetcode】486. Predict the Winner
  • 原文地址:https://www.cnblogs.com/liuwei0824/p/7428861.html
Copyright © 2020-2023  润新知