Python多进程multiprocessing使用示例
2016-06-10 14:07
543 查看
mutilprocess简介
像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多。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..')
View Code
主进程定义了一个Queue类型的变量,并作为Process的args参数传给子进程processA和processB,两个进程一个向队列中写数据,一个读数据。
相关文章推荐
- python+selenium环境搭建
- Python
- Python项目之万能的XML
- [转载]关闭Pycharm拼写检查
- Python编码爬坑指南(必看)
- python 基础之确认文件是否存在
- python threading
- Python 必选参数,默认参数,可变参数,关键字参数和命名关键字参数
- Python中的if __name__='__main__'语句的作用
- python学习之路-5 基础进阶篇
- 【转帖】pyspider:connect to scheduler rpc error: error(10061, '')问题解决方案
- python 系统级错误代码
- python 生成器总结
- 直接运行Python程序
- Panda3D学习之路(二)
- 最长递增自序列 python
- python regex
- python之路 模块,序列化,迭代器,生成器
- python的递归函数--含尾递归
- python中的切片--Splice