python 进程池Pool
2015-07-07 09:56
671 查看
#-*- coding:utf-8 -*- from multiprocessing import Pool import os,time,random def long_time_task(name): print 'Run task name %s and pid : %s..'%(name,os.getpid()) start = time.time() time.sleep(random.random()*3) end = time.time() print 'Task %s betwine %s '%(name,(end-start)) # pass if __name__ == '__main__': print 'Parent process %s.'%os.getpid() p=Pool() for i in range(5): print 'now I is ' and i print 'Parent process %s.'%os.getpid() p.apply_async(long_time_task,args=(i,)) print 'Wait for all subprocesses done...' print (i,) p.close() p.join() print 'All over'
结果(类):
Parent process 3896.
0
Parent process 3896.
1
Parent process 3896.
2
Parent process 3896.
3
Parent process 3896.
4
Parent process 3896.
Wait for all subprocesses done...
(4,)
Run task name 0 and pid : 4300..
Run task name 1 and pid : 4596..
Task 1 betwine 1.32100009918
Run task name 2 and pid : 4596..
Task 0 betwine 2.61500000954
Run task name 3 and pid : 4300..
Task 2 betwine 2.65400004387
Run task name 4 and pid : 4596..
Task 4 betwine 0.599999904633
Task 3 betwine 2.77799987793
All over
相关文章推荐
- 在Win7中使用Python的MySQLdb模块
- 在Win7中使用Python的MySQLdb模块
- Python学习之文件读写
- Python笔记:性能优化指南
- Python3爬虫爬取某网站美女图片
- yum 遇到 python运行问题
- Python自学笔记之函数式编程4——sorted
- Python 的一些高级特性
- boost.python编译及示例
- python unicode&str 转化
- Python中的urllib模块使用详解
- Python的多态性实例分析
- python生成IP段的方法
- python妹子图简单爬虫实例
- Python实现爬取逐浪小说的方法
- Python变量作用范围实例分析
- Python实现合并字典的方法
- python类和继承用法实例
- Python可变参数函数用法实例
- Python打印斐波拉契数列实例