python 多进程使用Queue通信的例子,pythonqueue,如下代码:import
python 多进程使用Queue通信的例子,pythonqueue,如下代码:import
如下代码:
import timefrom multiprocessing import Process,QueueMSG_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..'
主进程定义了一个Queue类型的变量,并作为Process的args参数传给子进程processA和processB,两个进程一个向队列中写数据,一个读数据。
其打印的结果如下:
C:\Python27\python.exe E:/outofmemory/test/queuetest/queuetest.pyprocessA start..processB start..queue is empty 0put hello world queue size is 1get msg hello worldqueue is empty 0queue is empty 0put hello world queue size is 1get msg hello worldqueue is empty 0queue is empty 0put hello world queue size is 1
相关内容
- python使用Connection实现多进程之间通信,pythonconnection,在
- python使用BeautifulSoup解析html获得网站的百度收录量,,
- 使用python对png图片文件做base64编码,,有时候我们需要使
- python根据进程名杀死进程,python杀死进程,在linux/unix平
- Python @staticmethod 和 @classmethod之间的区别,,使用@static
- Python 数组分隔总结,python数组分隔,初学Python被它的数
- 在python中调用外部命令,python调用外部命令,在python中有
- Python内存调优,python调优,在python中可以使用
- 查询关键词在百度排名python脚本分享,python脚本,如下脚
- python 的IO文件操作总结,,在项目开发过程中,时常需
评论关闭