多进程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 ...
随机推荐
- 洛谷P1170 兔八哥与猎人 欧拉函数的应用
https://www.luogu.org/problem/P1170 #include<bits/stdc++.h> using namespace std; ],b[],c[],d[] ...
- 微信公众号h5页面alert去掉域名
h5页面内嵌到微信公众号提示信息alert的时候会显示域名,去掉域名显示重写alert方法: window.alert = function(name){ var iframe = document. ...
- 【Python】字符串切片
- 在 linux 中连接 mysql 数据库
命令格式 mysql -h主机地址 -u用户名 -p用户密码 登录本机 mysql mysql -u用户名 -p用户密码 实例 TD - X1数据库:/opt/lampp/bin/mysql -u r ...
- Linux命令 sleep 延迟
用途说明 sleep命令常用于在Linux shell脚本中延迟时间 常用方式 注意:以下用法中<n>可以为小数. 格式:sleep <n> 格式:sleep <n> ...
- Winform中使用Reactivex代替BeginInvoke/Invoke来更新UI数据
首先通过Nuget安装包System.Reactive. ReactiveX项目 Url: https://github.com/Reactive-Extensions/Rx.NET public p ...
- 怎么把项目发布到github上
方法一:在github上新建一个项目,然后在本地任意个文件夹(最好新建)右键 git bash here ,再之后 git clone https://github.com/CKTim/BlueT ...
- 【原】mac电脑保存服务器账号/密码登录操作
一.说明 mac电脑自带终端神奇iterm2,日常登录服务器操作一般场景为ssh user@ip,确认后再输入密码操作. 该操作较为麻烦且需通过hostname判断所在主机. 通过ssh生成秘钥方式较 ...
- Atcoder Beginner Contest 155D(二分,尺取法,细节模拟)
二分,尺取法,细节模拟,尤其是要注意a[i]被计算到和a[i]成对的a[j]里时 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> ...
- 启动docker报Failed to start Docker Application Container Engine.解决
[root@docker ~]# systemctl status docker.service● docker.service - Docker Application Container Engi ...