设为首页 收藏本站
查看: 801|回复: 0

[经验分享] python 进程池1

[复制链接]

尚未签到

发表于 2015-4-27 09:47:58 | 显示全部楼层 |阅读模式
  有些情况下,所要完成的工作可以分解并独立地分布到多个工作进程,对于这种简单的情况,可以用Pool类来管理固定数目的工作进程。作业的返回值会收集并作为一个列表返回。(以下程序cpu数量为2,相关函数解释见python 进程池2 - Pool相关函数)。



1 import multiprocessing
2
3 def do_calculation(data):
4     return data*2
5 def start_process():
6     print 'Starting',multiprocessing.current_process().name
7
8 if __name__=='__main__':
9     inputs=list(range(10))
10     print 'Inputs  :',inputs
11
12     builtin_output=map(do_calculation,inputs)
13     print 'Build-In :', builtin_output
14
15     pool_size=multiprocessing.cpu_count()*2
16     pool=multiprocessing.Pool(processes=pool_size,
17         initializer=start_process,)
18
19     pool_outputs=pool.map(do_calculation,inputs)
20     pool.close()
21     pool.join()
22
23     print 'Pool  :',pool_outputs
  运行结果:



1 Inputs  : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 Build-In : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
3 Starting PoolWorker-2
4 Starting PoolWorker-1
5 Starting PoolWorker-3
6 Starting PoolWorker-4
7 Pool  : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
  
  默认情况下,Pool会创建固定数目的工作进程,并向这些工作进程传递作业,直到再没有更多作业为止。maxtasksperchild参数为每个进程执行task的最大数目,设置maxtasksperchild参数可以告诉池在完成一定数量任务之后重新启动一个工作进程,来避免运行时间很长的工作进程消耗太多的系统资源。
  maxtasksperchild is the number of tasks a worker process can complete before it will exit and be replaced with a fresh worker process, to enable unused resources to be freed. The default maxtasksperchild is None, which means worker processes will live as long as the pool.
Worker processes within a Pool typically live for the complete duration of the Pool’s work queue. A frequent pattern found in other systems (such as Apache, mod_wsgi, etc) to free resources held by workers is to allow a worker within a pool to complete only a set amount of work before being exiting, being cleaned up and a new process spawned to replace the old one. The maxtasksperchild argument to the Pool exposes this ability to the end user.
  
  notice
  python 2.6.6
  multiprocessing.Pool没有maxtaskperchild参数,Pool(processes=None, initializer=None, initargs=())
  
  python 2.7.3
  Pool(processes=None, initializer=None, initargs=(), maxtasksperchild=None)
  



1 import multiprocessing
2
3 def do_calculation(data):
4     return data*2
5 def start_process():
6     print 'Starting',multiprocessing.current_process().name
7
8 if __name__=='__main__':
9     inputs=list(range(10))
10     print 'Inputs  :',inputs
11
12     builtin_output=map(do_calculation,inputs)
13     print 'Build-In :', builtin_output
14
15     pool_size=multiprocessing.cpu_count()*2
16     pool=multiprocessing.Pool(processes=pool_size,
17         initializer=start_process,maxtasksperchild=2)
18
19     pool_outputs=pool.map(do_calculation,inputs)
20     pool.close()
21     pool.join()
22
23     print 'Pool  :',pool_outputs
  运行结果:



1 Inputs  : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 Build-In : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
3 Starting PoolWorker-1
4 Starting PoolWorker-2
5 Starting PoolWorker-3
6 Starting PoolWorker-4
7 Starting PoolWorker-5
8 Starting PoolWorker-6
9 Starting PoolWorker-7
10 Starting PoolWorker-8
11 Pool  : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
  池完成其所分配的任务时,即使没有更多的工作要做,也会重新启动工作进程。从这个输出可以看到,尽管只有10个任务,而且每个工作进程一次可以完成两个任务,但是这里创建了8个工作进程。
  
  更多的时候,我们不仅需要多进程执行,还需要关注每个进程的执行结果。



1 import multiprocessing
2 import time
3
4 def func(msg):
5     for i in xrange(3):
6         print msg
7         time.sleep(1)
8     return "done " + msg
9
10 if __name__ == "__main__":
11     pool = multiprocessing.Pool(processes=4)
12     result = []
13     for i in xrange(10):
14         msg = "hello %d" %(i)
15         result.append(pool.apply_async(func, (msg, )))
16     pool.close()
17     pool.join()
18     for res in result:
19         print res.get()
20     print "Sub-process(es) done."
  
  参考:
  《Python 标准库》 10.4.17 进程池(p445)
  http://www.coder4.com/archives/3352
  
  原文:http://www.iyunv.com/congbo/archive/2012/08/23/2652433.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61066-1-1.html 上篇帖子: python语法31[iterator和generator+yield] 下篇帖子: python类库26[读写mysql]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表