进程间的通信

方式1 queue队列 先进先出

进程之间默认是没有关联的,进程间的通信就是解决这个问题

from multiprocessing import Queue, Process这是解决普通进程间的通信
from multiprocessing import Manager, Pool这是解决进程间的通信

queue是阻塞的

from multiprocessing import Queue, Process
import os
import time

def write(q):
for value in [1, 2, 3]:
print("put %d to queue"%value)
q.put(value)
time.sleep(1)

def read(q):
while True:
if not q.empty():
value = q.get()
print("get %d from queue" % value)
time.sleep(1)
else:
break

if __name__ == "__main__":
queue =Queue()
pw = Process(target= write, args=(queue,)) ##这里就是把queue当作一个参数穿了进去。这样两个进程就和同一个queue相关联
pr = Process(target=read, args=(queue,))
pw.start()
pw.join()
pr.start()
pr.join()
print("complete")
Result:

put 1 to queue
put 2 to queue
put 3 to queue
get 1 from queue
get 2 from queue
get 3 from queue
complete

 

pipe()方式通信:

 

 

 

 

 

 

 

 

进程池之间的通信

from multiprocessing import Manager, Pool
import os
import time

def write(q):
for value in [1, 2, 3]:
print("put %d to queue"%value)
q.put(value)
time.sleep(1)

def read(q):
#while True:
#if not q.empty():
#value = q.get()
# print("get %d from queue" % value)
# time.sleep(1)
#else:
#break
  
  for value in range(q.qsize()):
  print("get %d from queue" % q.get())


if __name__ == "__main__":
q = Manager().Queue() #使用Manager中的Queue来初始化
po = Pool()
po.apply(write,(q,))#使用阻塞时创建进程, 这样就不用在read中使用死循环了
po.apply(read,(q,))
po.close()
po.join()
print("complete")
queue.Queue和multiprocessing.Queue是不同的:

Queue.Queue is just an in-memory queue that knows how to deal with multiple threads using it at the same time. It only works if both the producer and the consumer are in the same process.

Once you have them in separate system processes, which is what the multiprocessing library is about, things are a little more complicated, because the processes no longer share the same memory. You need some kind of inter-process communication method to allow the two processes to talk to each other. It can be a shared memory, a pipe or a socket, or possibly something else. This is what multiprocessing.Queue does. It uses pipes to provide a way for two processes to communicate. It just happens to implement the same API as Queue.Queue, because most Python programmers are already familiar with it.

Also note that the way you are using the queue, you have a race condition in your program. Think about what happens if the write process writes to the queue right after you call q.empty() in the read process. Normally you would add some special item to the queue (e.g. None) which would mean that the consumer can stop.

posted @ 2021-07-07 00:51  狼太白  阅读(59)  评论(0编辑  收藏  举报