python 多进程使用Queue通信的例子
2014-09-01 12:19
666 查看
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网络编程之TCP通信实例和socketserver框架使用例子
- python进程的通信:queue、进程池中的Queue
- python使用Queue在多个子进程间交换数据的方法
- python网络编程之TCP通信实例和socketserver框架使用例子
- [转] 最简单的使用UDP通信的Python Socket例子
- python进程与线程混合使用例子
- 在python中实现生产者和消费者的例子(二):使用multiprocessing和queue
- python队列、线程间通信Queue,多进程模块multiprocessing
- Python 进程通信,队列(multiprocessing.Queue()),单向通信
- python进程的通信:queue、进程池中的Queue
- python网络编程之TCP通信实例和socketserver框架使用例子
- python网络编程之TCP通信实例和socketserver框架使用例子
- 使用消息队列实现两进程间实时通信的例子
- 用python计算top命令中某个进程的CPU,Memory使用情况
- 16.1 进程间使用管道通信
- 使用Windows API实现两个进程间(含窗体)的通信
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- Python multiprocessing 使用手记[3] – 关于Queue
- python 进程通信测试代码(程序A,程序B)
- 进程通信——使用WM_COPYDATA消息通信