PYTHON多进程编码结束之进程池POOL
2015-11-26 09:06
656 查看
结束昨晚开始的测试。
最后一个POOL。
A,使用POOL的返回结果
B,多个进程池
截图:
最后一个POOL。
A,使用POOL的返回结果
#coding: utf-8 import multiprocessing import time def func(msg): print 'msg:', msg time.sleep(3) print 'end' return 'done', msg if __name__ == '__main__': pool = multiprocessing.Pool(processes=3) result = [] for i in xrange(4): msg = 'hello %d' %(i) result.append(pool.apply_async(func, (msg, ))) print 'Mark..Mark..Mark...' pool.close() pool.join() print 'Sub-process(es) done.' for res in result: print ':::', res.get()
B,多个进程池
#coding: utf-8 import multiprocessing import time, random, os def Lee(): print 'Run task Lee-%s' % (os.getpid()) start = time.time() time.sleep(random.random() * 10) end = time.time() print 'Task Lee, runs %0.2f seconds.' % (end - start) def Marlon(): print 'Run task Marlon-%s' % (os.getpid()) start = time.time() time.sleep(random.random() * 10) end = time.time() print 'Task Marlon, runs %0.2f seconds.' % (end - start) def Allen(): print 'Run task Allen-%s' % (os.getpid()) start = time.time() time.sleep(random.random() * 10) end = time.time() print 'Task Allen, runs %0.2f seconds.' % (end - start) def Frank(): print 'Run task Frank-%s' % (os.getpid()) start = time.time() time.sleep(random.random() * 10) end = time.time() print 'Task Frank, runs %0.2f seconds.' % (end - start) if __name__ == '__main__': function_list = [Lee, Marlon, Allen, Frank] print 'parent process %s' % (os.getpid()) pool = multiprocessing.Pool(4) for func in function_list: pool.apply_async(func) print 'Waiting for all subprocess done...' pool.close() pool.join() print 'All subprocesses done.'
截图:
相关文章推荐
- python 导入模块出错
- win7 64位 python3.4&opencv3.0配置安装教程
- Python yield深度解析
- python 入门
- Python_异常处理
- python核心编程(第二版)-------第六章课后习题
- python之路第五篇之模块和加密算法(进阶篇:续)
- python-29:多级页面爬取源码
- 关于字符串列表与列表的转换
- python 无定参数处理
- Python之collection系列
- Python基础——min/max与np.argmin/np.argmax
- leetcode之Plus One
- 如何将Mac OS X10.10.5下的Python2.7升级到最新的Python3.5
- Python开发简单记事本
- Pyserial+PyQT+Python3.4串口工具——界面
- python-excel操作之xlrd
- 机器学习实战-边学边读python代码(3)
- Python数据类型-----数字&字符串
- 前四课复习