python 多进程使用Queue通信的例子
- import time
- from multiprocessing import Process,Queue
- MSG_QUEUE = Queue(5)
- def startA(msgQueue):
- while True:
- if msgQueue.empty() > 0:
- print 'queue is empty %d' % (msgQueue.qsize())
- else:
- msg = msgQueue.get()
- print 'get msg %s' % (msg,)
- time.sleep(1)
- def startB(msgQueue):
- while True:
- msgQueue.put('hello world')
- print 'put hello world queue size is %d' % (msgQueue.qsize(),)
- time.sleep(3)
- if __name__ == '__main__':
- processA = Process(target=startA,args=(MSG_QUEUE,))
- processB = Process(target=startB,args=(MSG_QUEUE,))
- processA.start()
- print 'processA start..'
- processB.start()
- print 'processB start..'
- python2.6 test.py
- processA start..
- queue is empty 0
- processB start..
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
主进程定义了一个Queue类型的变量,并作为Process的args参数传给子进程processA和processB,两个进程一个向队列中写数据,一个读数据。
其打印的结果如下:
- python2.6 test.py
- processA start..
- queue is empty 0
- processB start..
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
- put hello world queue size is 1
- get msg hello world
- queue is empty 0
- queue is empty 0
python 多进程使用Queue通信的例子的更多相关文章
- python多进程之间的通信:消息队列Queue
python中进程的通信:消息队列. 我们知道进程是互相独立的,各自运行在自己独立的内存空间. 所以进程之间不共享任何变量. 我们要想进程之间互相通信,传送一些东西怎么办? 需要用到消息队列!! 进程 ...
- Python多进程multiprocessing使用示例
mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...
- Python 多进程编程之 进程间的通信(在Pool中Queue)
Python 多进程编程之 进程间的通信(在Pool中Queue) 1,在进程池中进程间的通信,原理与普通进程之间一样,只是引用的方法不同,python对进程池通信有专用的方法 在Manager()中 ...
- Python 多进程编程之 进程间的通信(Queue)
Python 多进程编程之 进程间的通信(Queue) 1,进程间通信Process有时是需要通信的,操作系统提供了很多机制来实现进程之间的通信,而Queue就是其中的一个方法----这是操作系统开辟 ...
- python多进程multiprocessing模块中Queue的妙用
最近的部门RPA项目中,小爬为了提升爬虫性能,使用了Python中的多进程(multiprocessing)技术,里面需要用到进程锁Lock,用到进程池Pool,同时利用map方法一次构造多个proc ...
- python MultiProcessing标准库使用Queue通信的注意要点
今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...
- python 中的queue 与多进程--待继续
一.先说说Queue(队列对象) Queue是python中的标准库,可以直接import 引用,之前学习的时候有听过著名的“先吃先拉”与“后吃先吐”,其实就是这里说的队列,队列的构造的时候可以定义它 ...
- Python多进程使用
[Python之旅]第六篇(六):Python多进程使用 香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要: 关于进程与线程的对比, ...
- python多进程(三)
消息队列 消息队列”是在消息的传输过程中保存消息的容器. 消息队列最经典的用法就是消费者和生成者之间通过消息管道来传递消息,消费者和生成者是不通的进程.生产者往管道中写消息,消费者从管道中读消息. ...
随机推荐
- nginx+tomcat多节点部署
在一台机器上想要将一个应用程序部署多个节点,可以通过nginx来实现. 1.将tomcat复制多份,修改tomcat配置文件conf/server.xml,将端口号设置成不一样的 2.将多个tomca ...
- apply 判定变量类型
js 数据类型 6大类:object ,undefined,boolean,string,number,null,但是有时候我们经常要更准确的判断,比如,是数组,还是单例... 那么就用apply吧, ...
- windows 短文件名/短路径名规则
How Windows Generates 8.3 File Names from Long File Names Windows generates short file names from lo ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- (转)Python爬虫学习笔记(2):Python正则表达式指南
以下内容转自CNBLOG:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并 ...
- 一行python打印乘法表
一行代码打印乘法表 >>> print '\n'.join([' '.join(['%s*%s=%-2s' %(y,x,x*y) for y in range(1,x+1)]) fo ...
- 01 Servlet & Jsp 技术概述
Servlet 介绍 servlet 是运行在web服务器或应用服务器上的java程序, 它是一个中间层, 负责连接来自web浏览器或其他http客户端的请求和HTTP服务器上的数据库或应用程序. 为 ...
- 破解idea注册码
添加 “0.0.0.0 account.jetbrains.com”到host, hosts位置:C:\Windows\System32\drivers\etc 获取注册码网址: http://ide ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.3——使用Robotium进行功能测试
问题: 你想要使用Robotium库测试activity. 解决方案: 增加Robotium依赖,编写自己的测试脚本. 讨论: Android Test Support Library提供类可以操作a ...
- Hadoop格式化HDFS报错java.net.UnknownHostException: centos64
异常描述 在对HDFS格式化,执行hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示: [shirdrn@localhost bin]$ hadoop na ...