python多进程

Python创建子进程

from multiprocessing import Process


def fun1(name, id):
    print('测试%s多进程' % name)
    print('子:%d' % id)


if __name__ == '__main__':
    process_num = 5  # 开启5个子进程执行fun1函数
    process_list = []
    for i in range(process_num):
        p = Process(target=fun1, args=('Python', i))  # 实例化进程对象--target:每个子进程要执行的函数,args:函数所需参数
        p.start()  # 启动进程
        process_list.append(p)

    for i in process_list:
        p.join()  # # 让主进程等待子进程执行完毕再继续执行

    print('结束测试')

输出:

测试Python多进程
子:0
测试Python多进程
子:1
测试Python多进程
子:3
测试Python多进程
子:2
测试Python多进程
子:4
结束测试

可以看出子进程是并发执行的,添加了join所以主进程一定是等子进程执行完后再执行
join 函数的功能是为了实现进程同步的,具体是 阻塞当前进程也就是主进程直到调用join函数的进程执行完成后继续执行当前进程。
若注释掉:

 # for i in process_list:
    #     p.join()  # # 让主进程等待子进程执行完毕再继续执行

则输出:

结束测试
测试Python多进程
子:0
测试Python多进程
子:1
测试Python多进程
子:2
测试Python多进程
子:3
测试Python多进程
子:4

Python进程间相互通信–使用Pipe实现消息队列

1、Pipe方法返回(conn1, conn2)代表一个管道的两个端。Pipe方法有duplex参数,如果duplex参数为True(默认值),那么这个管道是全双工模式,也就是说conn1和conn2均可收发。duplex为False,conn1只负责接收消息,conn2只负责发送消息。
2、send和recv方法分别是发送和接受消息的方法。close方法表示关闭管道,当消息接收结束以后,关闭管道。

from multiprocessing import Process, Pipe


def fun1(pipe):
    for i in range(10):
        pipe.send(i)
        print('fun1--send %d to pipe' % i)
        result = pipe.recv()
        print('fun1--recv %d from pipe' % (result))

def fun2(pipe):
    for i in range(10):
        result = pipe.recv()  # 如果没有数据传过来,就会一直等待,不会执行下一句
        print('fun2--recv %d from pipe' % (result+2))
        pipe.send((result+2))
        print('fun2--send %d to pipe' % (result+2))

if __name__ == '__main__':
    pipe = Pipe()
    p1 = Process(target=fun1, args=(pipe[1],))  # pipe[1]即conn2,当duplex=False时,conn2只负责发送数据
    p2 = Process(target=fun2, args=(pipe[0],))  # pipe[0]即conn1,当duplex=False时,conn1只负责接收数据
    p1.start()  # 启动进程
    p2.start()  # 启动进程
    p1.join()  # 执行完该子进程才能执行现在的主进程
    p2.join()
    pipe[0].close()
    pipe[1].close()
    print('结束测试')

输出:

fun1--send 0 to pipe
fun2--recv 2 from pipe
fun1--recv 2 from pipe
fun2--send 2 to pipefun1--send 1 to pipe
fun2--recv 3 from pipe
fun1--recv 3 from pipe
fun1--send 2 to pipe
fun2--send 3 to pipe
fun2--recv 4 from pipe
fun2--send 4 to pipe
fun1--recv 4 from pipe
fun1--send 3 to pipe
fun2--recv 5 from pipe
fun2--send 5 to pipe
fun1--recv 5 from pipe
fun1--send 4 to pipe
fun2--recv 6 from pipe
fun2--send 6 to pipe
fun1--recv 6 from pipe
fun1--send 5 to pipe
fun2--recv 7 from pipe
fun2--send 7 to pipe
fun1--recv 7 from pipe
fun1--send 6 to pipe
fun2--recv 8 from pipe
fun2--send 8 to pipe
fun1--recv 8 from pipe
fun1--send 7 to pipe
fun2--recv 9 from pipe
fun2--send 9 to pipe
fun1--recv 9 from pipe
fun1--send 8 to pipe
fun2--recv 10 from pipe
fun2--send 10 to pipe
fun1--recv 10 from pipe
fun1--send 9 to pipe
fun2--recv 11 from pipe
fun2--send 11 to pipe
fun1--recv 11 from pipe
结束测试
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值