1. 21python中的进程间通信、线程间通信与生产者消费者模型_笔记
    2. # multiprocessing.Queue
    3. # 进程彼此之间互相隔离,要实现进程间通信(IPC),
    4. # multiprocessing模块支持两种形式:队列Queue和管道Pipe,这两种方式都是使用消息传递的
    5. # 创建队列的类(底层就是以管道和锁定的方式实现):
    6. # Queue([maxsize]):创建共享的进程队列,Queue是多进程安全的队列,可以使用Queue实现多进程之间的数据传递。
    7. # 参数介绍:maxsize是队列中允许最大项数,省略则无大小限制。
    8. # Queue介绍方法
    9. # q.get_nowait():同q.get(False)
    10. # q.put_nowait():同q.put(False)
    11. # q.empty():调用此方法时q为空则返回True,该结果不可靠,比如在返回True的过程中,如果队列中又加入了项目。
    12. # q.full():调用此方法时q已满则返回True,该结果不可靠,比如在返回True的过程中,如果队列中的项目被取走。
    13. # q.qsize():返回队列中目前项目的正确数量,结果也不可靠,理由同上.
    14. # pipe管道通信
    15. # Pipe方法返回(conn1, conn2)代表一个管道的两个端。
    16. # Pipe方法有duplex参数:duplex 为 True(默认值),那么这个管道是全双工模式,也就是说conn1和conn2均可收发。
    17. # duplex 为 False,conn1只负责接受消息,conn2只负责发送消息。前收后发
    18. # send和recv方法分别是发送和接收消息的方法。
    19. # 在全双工模式下,可以调用conn1.send发送消息,conn1.recv接收消息。
    20. # 如果没有消息可接收,recv方法会一直阻塞。如果管道已经被关闭,那么recv方法会抛出EOFError
    21. # -*- coding: utf-8 -*-
    22. __author__ = 'dongfangyao'
    23. __date__ = '2018/1/17 下午9:40'
    24. __product__ = 'PyCharm'
    25. __filename__ = 'processing3'
    26. import time
    27. import multiprocessing
    28. # 进程间通信 发消息 数据
    29. # 队列 管道 Queue Pipe
    30. # put get
    31. # queue实现跨进程通信
    32. # def put(q):
    33. # for value in ['a', 'b', 'c']:
    34. # print('发送 %s 到queue中。。。'%value)
    35. # q.put(value)
    36. # time.sleep(2)
    37. #
    38. #
    39. # def get(q):
    40. # while True:
    41. # value = q.get(True)
    42. # print('从queue取到数据:%s !'%value)
    43. #
    44. #
    45. # q = multiprocessing.Queue()
    46. #
    47. # pwrite = multiprocessing.Process(target=put, args=(q, ))
    48. # pread = multiprocessing.Process(target=get, args=(q, ))
    49. #
    50. # pwrite.start()
    51. # pread.start()
    52. #
    53. # pwrite.join()
    54. # pread.terminate()
    55. #
    56. # print('父进程结束')
    57. # Pipe管道的方式 跨进程通信
    58. def put(p):
    59. for value in ['a', 'b', 'c']:
    60. print('发送 %s 到pipe中。。。'%value)
    61. p[1].send(value)
    62. # q.put(value)
    63. time.sleep(2)
    64. def get(p):
    65. while True:
    66. # value = q.get(True)
    67. value = p[0].recv()
    68. print('从pipe取到数据:%s !'%value)
    69. # Pipe()方法返回(conn1,conn2) 代表是一个管道的两端
    70. # 前收 后发
    71. p = multiprocessing.Pipe(duplex=False)
    72. pwrite = multiprocessing.Process(target=put, args=(p, ))
    73. pread = multiprocessing.Process(target=get, args=(p, ))
    74. pwrite.start()
    75. pread.start()
    76. pwrite.join()
    77. pread.terminate()
    78. print('父进程结束')
    79. 复制代码
    80. # 1、queue.Queue是进程内非阻塞队列 其实就是线程之间的通信
    81. # 2、multiprocess.Queue是跨进程通信队列。
    82. # 队列:先进先出 后进后出
    83. # 生成者消费者模型 是通过线程来实现的
    84. # -*- coding: utf-8 -*-
    85. __author__ = 'dongfangyao'
    86. __date__ = '2018/1/17 下午9:54'
    87. __product__ = 'PyCharm'
    88. __filename__ = 'threading3'
    89. import time
    90. import threading
    91. import queue
    92. # 线程间通信 队列
    93. # queue.Queue()
    94. # 操作系统课程 生产者与消费者模式
    95. q = queue.Queue(maxsize=10)
    96. def producer(name):
    97. count = 1
    98. while True:
    99. q.put('骨头%s'%count)
    100. print('%s生产了骨头%s'%(name, count))
    101. count+=1
    102. time.sleep(0.5)
    103. def consumer(name):
    104. while True:
    105. print('[%s]消费[%s]并且吃了它...'%(name, q.get()))
    106. time.sleep(2)
    107. p = threading.Thread(target=producer, args=('dfy', ))
    108. c1 = threading.Thread(target=consumer, args=('zhangsan', ))
    109. c2 = threading.Thread(target=consumer, args=('lisi', ))
    110. p.start()
    111. c1.start()
    112. c2.start()
    113. 复制代码