1. import time
  2. from multiprocessing import Process,Queue
  3.  
  4. MSG_QUEUE = Queue(5)
  5.  
  6. def startA(msgQueue):
  7. while True:
  8. if msgQueue.empty() > 0:
  9. print 'queue is empty %d' % (msgQueue.qsize())
  10. else:
  11. msg = msgQueue.get()
  12. print 'get msg %s' % (msg,)
  13. time.sleep(1)
  14.  
  15. def startB(msgQueue):
  16. while True:
  17. msgQueue.put('hello world')
  18. print 'put hello world queue size is %d' % (msgQueue.qsize(),)
  19. time.sleep(3)
  20.  
  21. if __name__ == '__main__':
  22. processA = Process(target=startA,args=(MSG_QUEUE,))
  23. processB = Process(target=startB,args=(MSG_QUEUE,))
  24.  
  25. processA.start()
  26. print 'processA start..'
  27.  
  28. processB.start()
  29. print 'processB start..'
  1. python2.6 test.py
  2. processA start..
  3. queue is empty 0
  4. processB start..
  5. put hello world queue size is 1
  6. get msg hello world
  7. queue is empty 0
  8. queue is empty 0
  9. put hello world queue size is 1
  10. get msg hello world
  11. queue is empty 0
  12. queue is empty 0
  13. put hello world queue size is 1
  14. get msg hello world
  15. queue is empty 0
  16. queue is empty 0
  17. put hello world queue size is 1
  18. get msg hello world
  19. queue is empty 0
  20. queue is empty 0
  21. put hello world queue size is 1
  22. get msg hello world
  23. queue is empty 0
  24. queue is empty 0
  25. put hello world queue size is 1
  26. get msg hello world
  27. queue is empty 0
  28. queue is empty 0
  29. put hello world queue size is 1
  30. get msg hello world
  31. queue is empty 0
  32. queue is empty 0

主进程定义了一个Queue类型的变量,并作为Process的args参数传给子进程processA和processB,两个进程一个向队列中写数据,一个读数据。

其打印的结果如下:

  1. python2.6 test.py
  2. processA start..
  3. queue is empty 0
  4. processB start..
  5. put hello world queue size is 1
  6. get msg hello world
  7. queue is empty 0
  8. queue is empty 0
  9. put hello world queue size is 1
  10. get msg hello world
  11. queue is empty 0
  12. queue is empty 0
  13. put hello world queue size is 1
  14. get msg hello world
  15. queue is empty 0
  16. queue is empty 0
  17. put hello world queue size is 1
  18. get msg hello world
  19. queue is empty 0
  20. queue is empty 0
  21. put hello world queue size is 1
  22. get msg hello world
  23. queue is empty 0
  24. queue is empty 0
  25. put hello world queue size is 1
  26. get msg hello world
  27. queue is empty 0
  28. queue is empty 0
  29. put hello world queue size is 1
  30. get msg hello world
  31. queue is empty 0
  32. queue is empty 0

python 多进程使用Queue通信的例子的更多相关文章

  1. python多进程之间的通信:消息队列Queue

    python中进程的通信:消息队列. 我们知道进程是互相独立的,各自运行在自己独立的内存空间. 所以进程之间不共享任何变量. 我们要想进程之间互相通信,传送一些东西怎么办? 需要用到消息队列!! 进程 ...

  2. Python多进程multiprocessing使用示例

    mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...

  3. Python 多进程编程之 进程间的通信(在Pool中Queue)

    Python 多进程编程之 进程间的通信(在Pool中Queue) 1,在进程池中进程间的通信,原理与普通进程之间一样,只是引用的方法不同,python对进程池通信有专用的方法 在Manager()中 ...

  4. Python 多进程编程之 进程间的通信(Queue)

    Python 多进程编程之 进程间的通信(Queue) 1,进程间通信Process有时是需要通信的,操作系统提供了很多机制来实现进程之间的通信,而Queue就是其中的一个方法----这是操作系统开辟 ...

  5. python多进程multiprocessing模块中Queue的妙用

    最近的部门RPA项目中,小爬为了提升爬虫性能,使用了Python中的多进程(multiprocessing)技术,里面需要用到进程锁Lock,用到进程池Pool,同时利用map方法一次构造多个proc ...

  6. python MultiProcessing标准库使用Queue通信的注意要点

    今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...

  7. python 中的queue 与多进程--待继续

    一.先说说Queue(队列对象) Queue是python中的标准库,可以直接import 引用,之前学习的时候有听过著名的“先吃先拉”与“后吃先吐”,其实就是这里说的队列,队列的构造的时候可以定义它 ...

  8. Python多进程使用

    [Python之旅]第六篇(六):Python多进程使用   香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要:   关于进程与线程的对比, ...

  9. python多进程(三)

    消息队列 消息队列”是在消息的传输过程中保存消息的容器. 消息队列最经典的用法就是消费者和生成者之间通过消息管道来传递消息,消费者和生成者是不通的进程.生产者往管道中写消息,消费者从管道中读消息.   ...

随机推荐

  1. nginx+tomcat多节点部署

    在一台机器上想要将一个应用程序部署多个节点,可以通过nginx来实现. 1.将tomcat复制多份,修改tomcat配置文件conf/server.xml,将端口号设置成不一样的 2.将多个tomca ...

  2. apply 判定变量类型

    js 数据类型 6大类:object ,undefined,boolean,string,number,null,但是有时候我们经常要更准确的判断,比如,是数组,还是单例... 那么就用apply吧, ...

  3. windows 短文件名/短路径名规则

    How Windows Generates 8.3 File Names from Long File Names Windows generates short file names from lo ...

  4. Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法

    /** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...

  5. (转)Python爬虫学习笔记(2):Python正则表达式指南

    以下内容转自CNBLOG:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并 ...

  6. 一行python打印乘法表

    一行代码打印乘法表 >>> print '\n'.join([' '.join(['%s*%s=%-2s' %(y,x,x*y) for y in range(1,x+1)]) fo ...

  7. 01 Servlet & Jsp 技术概述

    Servlet 介绍 servlet 是运行在web服务器或应用服务器上的java程序, 它是一个中间层, 负责连接来自web浏览器或其他http客户端的请求和HTTP服务器上的数据库或应用程序. 为 ...

  8. 破解idea注册码

    添加 “0.0.0.0 account.jetbrains.com”到host, hosts位置:C:\Windows\System32\drivers\etc 获取注册码网址: http://ide ...

  9. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.3——使用Robotium进行功能测试

    问题: 你想要使用Robotium库测试activity. 解决方案: 增加Robotium依赖,编写自己的测试脚本. 讨论: Android Test Support Library提供类可以操作a ...

  10. Hadoop格式化HDFS报错java.net.UnknownHostException: centos64

    异常描述 在对HDFS格式化,执行hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示: [shirdrn@localhost bin]$ hadoop na ...