python 学习笔记(Queue & Pipe 进程间的通讯)

Pipe

multiprocessing.Pipe()即管道模式,调用Pipe()返回管道的两端的Connection。
pipe()方法返回的是一个tuple(conn1,conn2);
Pipe方法还有一个参数duplex参数,如果deplex=True,叫双工模式;deplex=False单工模式,那么返回的参数conn1只负责接受,conn2只负责发信息。

主要方法:

(1)send() 发信息
(2)rece()接受信息
(3)close()关闭管道

例子(1)单进程

from multiprocessing import  Pipe,Process

def son_process(x,pipe):
    _out_pipe,_in_pipe = pipe

    while x:
        try:
            #recv 接受信息
            msg = _out_pipe.recv()
            print(msg)
            x = x-1
        except EOFError:
            break

if __name__ == '__main__':
    out_pipe,in_pipe = Pipe(True)
    son_p = Process(target=son_process,args=(10,(out_pipe,in_pipe)))
    son_p.start()

    #out_pipe,close()
    for x in range(10):
        #send 发信息
        in_pipe.send(x)
    son_p.join()
    print("主进程也结束了")

运行结果:

0
1
2
3
4
5
6
7
8
9
主进程也结束了

例子(2)多进程之间通讯

import time

import multiprocessing

from multiprocessing import Process


def proc1(pipe):
    for i in range(10):
        print("send {0}".format(i))
        pipe.send(i)
        time.sleep(0.1)

def proc2(pipe):
    n = 10
    while n:
        print("proc2 recv: {0}".format(pipe.recv()))
        n -= 1


if __name__ == '__main__':
    (p1, p2) = multiprocessing.Pipe(duplex=False)
    pr = Process(target=proc1, args=(p2,))
    cu = Process(target=proc2, args=(p1,))
    pr.start()
    cu.start()

运行结果:

send 0
proc2 recv: 0
send 1
proc2 recv: 1
send 2
proc2 recv: 2
send 3
proc2 recv: 3
send 4
proc2 recv: 4
send 5
proc2 recv: 5
send 6
proc2 recv: 6
send 7
proc2 recv: 7
send 8
proc2 recv: 8
send 9
proc2 recv: 9

multiprocessing.Queue 队列

from Queue import Queue

常用方法

Queue(maxsize=0) 指定队列大小,0表示无限
q = Queue()
q.qsize() #返回当前队列的空间
q.empty() #判断当前队列是否为空
q.full() #判断当前队列是否满
q.put() #放消息
q.get() #获取消息
q.task_done() #接受消息的线程调用该函数来说明消息对应的任务是否已经完成
q.join() #等待队列为空,在执行别的操作

例子

from multiprocessing import  Queue,Pipe
from threading import Thread
import  time

#生产
def produce(q):
    print("start produce")
    for i in range(10):
        q.put(i) #放入消息
        print("produce has produced value {0}".format(i))
        time.sleep(0.3)
    print("end produce")

#消费
def custome(q):
    print("start custorme")
    while True:
        data = q.get() #获取消息
        print("custormer has get value{0}".format(data))

if __name__ =="__main__":
    q = Queue()
    p = Thread(target=produce,args=(q,))
    c = Thread(target=custome,args=(q, ))
    p.start()
    c.start()

运行结果:

start produce
start custorme
produce has produced value 0
custormer has get value0
produce has produced value 1
custormer has get value1
produce has produced value 2
custormer has get value2
produce has produced value 3
custormer has get value3
produce has produced value 4
custormer has get value4
produce has produced value 5
custormer has get value5
produce has produced value 6
custormer has get value6
produce has produced value 7
custormer has get value7
produce has produced value 8
custormer has get value8
produce has produced value 9
custormer has get value9
end produce

你可能感兴趣的:(python 学习笔记(Queue & Pipe 进程间的通讯))