pipe模块可以实现进程之间数据传递

栗子1:3个进程,一个主进程,2个子进程,三个管道,三个进程通过3个管道连接,主进程发一个信息,通过2个子进程转发,最后回到主进程输出

import multiprocessing

def func(pipe_end,pipe_head):
msg = pipe_end.recv()
print("--->",msg)
pipe_head.send(msg) if __name__ == '__main__':
pipe_head_1,pipe_end_1 = multiprocessing.Pipe()#声明管道对1
pipe_head_2,pipe_end_2 = multiprocessing.Pipe()#声明管道对2
pipe_head_3,pipe_end_3 = multiprocessing.Pipe()#声明管道对3 p1 = multiprocessing.Process(target=func,args=(pipe_end_1,pipe_head_2))#子进程p1 执行函数func,参数为管道两端
p1.start()
p2 = multiprocessing.Process(target=func,args=(pipe_end_2,pipe_head_3))#子进程p2 执行函数func,参数为管道两端
p2.start()
pipe_head_1.send("hello")
print(pipe_end_3.recv())

栗子2:3个进程,一个主进程,一个子进程,一个孙子进程,三个进程通过3个管道连接,主进程发一个信息,通过2个子进程转发,最后回到主进程输出

import multiprocessing

def func(pipe_end,pipe_head,*args):
#print("--->",args[0])
#print('--->',args[1])
msg = pipe_end.recv()
print("-->",msg)
pipe_head.send(msg)
p2 = multiprocessing.Process(target=func2,args=(args[0],args[1]))#子进程p2 执行函数func2,参数为管道两端
p2.start() def func2(pipe_end,pipe_head):
msg = pipe_end.recv()
print("--->",msg)
pipe_head.send(msg) if __name__ == '__main__':
pipe_head_1,pipe_end_1 = multiprocessing.Pipe()#声明管道对1
pipe_head_2,pipe_end_2 = multiprocessing.Pipe()#声明管道对2
pipe_head_3,pipe_end_3 = multiprocessing.Pipe()#声明管道对3
#print(pipe_end_2)
#print(pipe_head_3) p1 = multiprocessing.Process(target=func,args=(pipe_end_1,pipe_head_2,pipe_end_2,pipe_head_3))#子进程p1 执行函数func,参数为管道两端
p1.start() pipe_head_1.send("hello")
print("->",pipe_end_3.recv())

多进程pipe的更多相关文章

  1. 多进程-Pipe和Manager数据共享和传递

    pipe.py#多进程数据传递接收和发送(类似socket) from multiprocessing import Process,Pipe def f(conn): conn.send([42,N ...

  2. python学习笔记——multiprocessing 多进程组件 Pipe管道

    进程间通信(IPC InterProcess Communication)是值在不同进程间传播或交换信息. IPC通过有管道(无名管道 和 有名 / 命名管道).消息队列.共享存储 / 内容.信号量. ...

  3. [b0037] python 归纳 (二二)_多进程数据共享和同步_管道Pipe

    # -*- coding: utf-8 -*- """ 多进程数据共享 管道Pipe 逻辑: 2个进程,各自发送数据到管道,对方从管道中取到数据 总结: 1.只适合两个进 ...

  4. python类库32[多进程通信Queue+Pipe+Value+Array]

    多进程通信 queue和pipe的区别: pipe用来在两个进程间通信.queue用来在多个进程间实现通信. 此两种方法为所有系统多进程通信的基本方法,几乎所有的语言都支持此两种方法. 1)Queue ...

  5. Python多进程编程-进程间协作(Queue、Lock、Semaphore、Event、Pipe)

    进程与进程之间是相互独立的,互不干扰.如果多进程之间需要对同一资源操作,就需要进程间共享变量,上一篇文章介绍了进程间共享数据的三大类Value.Array.Manager,这三种类的主要区别在于管理的 ...

  6. python多进程程序之间交换数据的两种办法--Queue和Pipe

    合在一起作的测试. #!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessing import random import ...

  7. 多进程小例子--fork+pipe

    1 #include<stdio.h>  2 #include<unistd.h>  3   4 #define m 6  5 int main()  6 {  7       ...

  8. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  9. python高级之多进程

    python高级之多进程 本节内容 多进程概念 Process类 进程间通讯 进程同步 进程池 1.多进程概念 multiprocessing is a package that supports s ...

随机推荐

  1. Apache NIFI

    Add a.password file to chrome. Settings -> Advanced -> Security -> Manage Certificates -> ...

  2. java多线程技术

    如何实现线程 首先实现线程的两个方法:1.继承thread:2.实现接口Runnable类: 这边我就说一下第二种,因此第二种在开发中使用的比较多一些,能避免继承还是少避免继承. RunnableDe ...

  3. python3练习100题——008

    今天第二道,做了明天就可以休息一下- 原题链接:http://www.runoob.com/python/python-exercise-example8.html 题目:输出 9*9 乘法口诀表. ...

  4. tomcat8.5和redis实现session共享

    1. 问题 ​ 由于之前看其他资料配置的session共享没注意自己tomcat的版本所以出现了诸多问题,tomcat8.5和之前版本的配置是不一样的. 2. 配置 ​ ①将如图所示三个jar包放入t ...

  5. unity的一些特殊目录

    Hidden Folders Folders that start with a dot (e.g. ".UnitTests/", ".svn/") are i ...

  6. bbs论坛登录相关功能(2)

    昨天把注册功能页面做出来,接下来就是登录页面 登录功能: 1,用户账号,密码后台效验,错误信息在登录按钮右边显示 2.验证码,根据图片生成,点击图片刷新产生新的验证码 修改密码 注册 先把前端页面lo ...

  7. centOS7中启动MySQL数据库提示: Failed to start mysqld.service: Unit not foundc

    现象: 在centOS7中启动MySQL数据库提示: Failed to start mysqld.service: Unit not found [明明已经安装了,为什么提示不存在呢?] 原因: 在 ...

  8. 使用命令把SpringBoot项目打包成可运行的jar包(简洁,操作性强)

    前几天接到一个需求,要把原系统中一个数据处理小功能搬出原系统,拉出来单独做一个SpringBoot项目,然后打成jar包扔到Windows服务器上运行,这样数据处理的时候如果遇到堵塞就不至于整个系统都 ...

  9. IntelliJ IDEA如何导入jar包

    转自:https://jingyan.baidu.com/article/0f5fb0993e9e1f6d8334ead2.html 通过这种方式导入jar包,idea就能百分百识别到,如果是那种直接 ...

  10. testng如何实现用例间依赖

    todo: 参考: https://www.cnblogs.com/znicy/p/6534893.html