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多进程(三)
消息队列 消息队列”是在消息的传输过程中保存消息的容器. 消息队列最经典的用法就是消费者和生成者之间通过消息管道来传递消息,消费者和生成者是不通的进程.生产者往管道中写消息,消费者从管道中读消息. ...
随机推荐
- dbutils使用---QueryRunner、BeanListHandler、BeanHandler、MapListHandler、MapHandler、ScalarHandler
1. ResultSetHandler 的作用: QueryRunner 的 query 方法的返回值最终取决于 query 方法的 ResultHandler 参数的 hanlde 方法的返回值. ...
- ExtPager ,分页
package cn.edu.hbcf.common.vo; public class ExtPager { private Integer start; private Integer limit; ...
- 本地运行storm时报错
java.lang.NoClassDefFoundError: backtype/storm/topology/IRichSpout at java.lang.Class.getDeclaredMet ...
- android最佳实践的建议(翻译自android-best-practices)
Best practices in Android development Use Gradle and its recommended project structure 使用Gradle和其推荐的 ...
- jQuery插件学习笔记
近期在研究jQuery插件,插件编写的目的是给已经有的一系列方法或函数做一个封装,以便在其它地方反复使用,方便后期维护. JQuery除了提供一个简单.有效的方式进行管理元素以及脚本,它还还提供了例外 ...
- 分析kube-proxy的iptables规则
NodePort service 创建一个mysql的NodePort服务,对应两个pod实例,rc和service的配置如下: 1.rc配置 apiVersion: v1 kind: Replica ...
- sed awk文本处理教程
sed全名叫stream editor,流编辑器,用程序的方式来编辑文本,相当的hacker啊.sed基本上就是玩正则模式匹配,所以,玩sed的人,正则表达式一般都比较强. 把my字符串替换成Hao ...
- pl/sql 实例精解 04
本章主要讨论, IF 语句的应用. 1: if condition1 then 2: statement1 3: elsif condition2 then 4: statement2 5: else ...
- 【vijos】1791 骑士的旅行(特殊的技巧)
https://vijos.org/p/1791 暴力的话只想到bfs,然后估计是状态超了才得20分. 噗,为啥暴力就不能想得简单点QAQ.....这种思想很好啊. 这一题我看了题解后不得不说我竟然没 ...
- 在Intellij IDEA下用X-debug调试PHP
用Intellij IDEA使用X-debug来调试PHP,主要需要配置的部分有三个地方,分别为php.ini的配置,IDEA的配置和浏览器的配置,主要如下: php.ini(wamp修改的是phpF ...