多进程pipe
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的更多相关文章
- 多进程-Pipe和Manager数据共享和传递
pipe.py#多进程数据传递接收和发送(类似socket) from multiprocessing import Process,Pipe def f(conn): conn.send([42,N ...
- python学习笔记——multiprocessing 多进程组件 Pipe管道
进程间通信(IPC InterProcess Communication)是值在不同进程间传播或交换信息. IPC通过有管道(无名管道 和 有名 / 命名管道).消息队列.共享存储 / 内容.信号量. ...
- [b0037] python 归纳 (二二)_多进程数据共享和同步_管道Pipe
# -*- coding: utf-8 -*- """ 多进程数据共享 管道Pipe 逻辑: 2个进程,各自发送数据到管道,对方从管道中取到数据 总结: 1.只适合两个进 ...
- python类库32[多进程通信Queue+Pipe+Value+Array]
多进程通信 queue和pipe的区别: pipe用来在两个进程间通信.queue用来在多个进程间实现通信. 此两种方法为所有系统多进程通信的基本方法,几乎所有的语言都支持此两种方法. 1)Queue ...
- Python多进程编程-进程间协作(Queue、Lock、Semaphore、Event、Pipe)
进程与进程之间是相互独立的,互不干扰.如果多进程之间需要对同一资源操作,就需要进程间共享变量,上一篇文章介绍了进程间共享数据的三大类Value.Array.Manager,这三种类的主要区别在于管理的 ...
- python多进程程序之间交换数据的两种办法--Queue和Pipe
合在一起作的测试. #!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessing import random import ...
- 多进程小例子--fork+pipe
1 #include<stdio.h> 2 #include<unistd.h> 3 4 #define m 6 5 int main() 6 { 7 ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- python高级之多进程
python高级之多进程 本节内容 多进程概念 Process类 进程间通讯 进程同步 进程池 1.多进程概念 multiprocessing is a package that supports s ...
随机推荐
- Python2安装MySQLdb
在http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python下载对应的包版本,如果是win7 64位2.7版本的python,就下载 MySQL_p ...
- EntityFramework之原始查询及性能优化
之前做海信项目,数据量自交大,为了提高查询效率用的 https://www.cnblogs.com/CreateMyself/p/4746258.html
- PP: Data-driven classification of residential energy consumption patterns by means of functional connectivity networks
Purpose Implement a good user aggregation and classification. or to assess the interrelation pattern ...
- C语言-防止输入字母
今天群里一位小伙伴问了一个关于scanf函数的问题: scanf("%d", &n); 这个代码怎么防止输入字母? 因为他下面是判断n是否为质数,所以这里肯定有个判断,不然 ...
- java-日期取特定值
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @author G ...
- mediasoup-demo解析-客户端
1.gulp任务管理 app官方文档 npm start启动app,执行脚本: "start": "gulp live" 该命令用启动gulp顺序任务组合liv ...
- Resharper快捷键使用
1: Alt+F7将你光标所在位置的变量的所有使用以列表的方式显示出来,显示结果的窗体可以像其他窗体那样停靠. 它的优点包括: 可以从所有使用中挑选只显示read usage或者write usa ...
- [学习笔记]用Python简易向喜欢的人表白
前几天是情人节,就用Python图像库PIL来搞点事情. 先看图: 其实这样看不出什么来,然后需要放大: 放大以后就能看到你相对女神说的话. 但是对于学计算机的我来说,更想琢磨是怎样的流程完成的这个图 ...
- html+css 文本折叠
先看效果: 收缩状态 展开状态 源代码: <!doctype html> <html lang="zh"> <head> <meta ch ...
- 【做题笔记】[NOIOJ,非NOIp原题]装箱问题
题意:给定一些矩形,面积分别是 \(1\times 1,2\times 2,3\times 3,4\times 4,5\times 5,6\times 6\).您现在知道了这些矩形的个数 \(a,b, ...