python学习—Day42—多进程Manager,进程池
来源:互联网 发布:mac10.7.5版本软件 编辑:程序博客网 时间:2024/05/22 01:47
多进程的Manager:
强大的Manager:;python中提供了强大的Manager专门用来做数据共享的,其支持的类型非常多,包括,value,array,list,dict,queue,lock等。
例子:
#@File :jincheng_manager.pyimport multiprocessingdef worker(d, l): l += range(11, 16) for i in xrange(1, 6): key = "key{0}".format(i) val = "val{0}".format(i) d[key] = valif __name__ == "__main__": manager = multiprocessing.Manager() d = manager.dict() l = manager.list() p = multiprocessing.Process(target=worker, args=(d, l)) p.start() p.join() print(d) print(l) print("main end")
{'key3': 'val3', 'key2': 'val2', 'key1': 'val1', 'key5': 'val5', 'key4': 'val4'}[11, 12, 13, 14, 15]
main end
多进程的进程池:
pool可以提供指定数量的进程,供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;但如果池中的进程数已经达到规定的最大值,那么该请求就会等待,直到池中有进程结束,才会创建新的进程。
#@File :pool.pyimport multiprocessingimport timedef worker(msg): print("########### start {0} ##########".format(msg)) time.sleep(1) print("########### end {0} ##########".format(msg))if __name__ == "__main__": pool = multiprocessing.Pool(processes=3) for i in xrange(1, 10): msg = "hello {0}".format(i) pool.apply_async(func=worker, args=(msg, )) # pool.apply(func=worker, args=(msg, )) #这句与上面一句区别:上面是非阻塞,最大限度利用pool,这句是只开一个窗口。 pool.close() pool.join() #在join之前一定要close,否则报错。 print("main end")
########### start hello 1 ##################### start hello 2 ##########
########### start hello 3 ##########
########### end hello 2 ##########
########### end hello 1 ##########
########### start hello 4 ##########
########### start hello 5 ##########
########### end hello 3 ##########
########### start hello 6 ##########
########### end hello 4 ##########
########### end hello 5 ##########
########### start hello 7 ##########
########### start hello 8 ##########
########### end hello 6 ##########
########### start hello 9 ##########
########### end hello 7 ##################### end hello 8 ##########
########### end hello 9 ##########
main end
阅读全文
0 0
- python学习—Day42—多进程Manager,进程池
- python学习——多进程
- python学习——多进程
- python学习—Day40—多进程
- python Manager对象增加进程
- python多进程学习
- Python多进程——进程池Pool
- python类库32[多进程共享高级之Manager]
- Python——多进程
- Python 多进程池的学习
- python学习——进程和线程
- python学习——进程vs线程
- python学习——分布式进程
- python学习—Day41—多进程锁与多进程共享内存
- Python学习笔记——多线程,多进程
- <28>python学习笔记——多线程 / 多进程
- Python中的多进程学习
- python 多进程 —— 进程间通信
- SSL基础知识
- 小白科普:Netty有什么用?
- 多线程作业
- python的GUI,Label和Scale联合使用
- 投资要注意风控
- python学习—Day42—多进程Manager,进程池
- 工程计算5——线性方程组的迭代解法
- 唯快不破:UNIX网络编程--非阻塞connect的实现
- react获取URL中参数
- 应用层API和驱动之间的关系
- 引用类型(三) Date类型
- java -- 模板设计、复合模式、单例设计
- python爬虫学习 之 定向爬取 股票信息
- 手机版common.css