python 多进程并行编程 ProcessPoolExecutor的实现


Posted in Python onOctober 11, 2019

使用 ProcessPoolExecutor

from concurrent.futures import ProcessPoolExecutor, as_completed
import random

斐波那契数列

当 n 大于 30 时抛出异常

def fib(n):
  if n > 30:
    raise Exception('can not > 30, now %s' % n)
  if n <= 2:
    return 1
  return fib(n-1) + fib(n-2)

准备数组

nums = [random.randint(0, 33) for _ in range(0, 10)]
'''
[13, 17, 0, 22, 19, 33, 7, 12, 8, 16]
'''

方案一:submit

submit 输出结果按照子进程执行结束的先后顺序,不可控

with ProcessPoolExecutor(max_workers=3) as executor:
    futures = {executor.submit(fib, n):n for n in nums}
    for f in as_completed(futures):
      try:
        print('fib(%s) result is %s.' % (futures[f], f.result()))
      except Exception as e:
        print(e)
'''
fib(13) result is 233.
fib(17) result is 1597.
fib(0) result is 1.
fib(22) result is 17711.
fib(19) result is 4181.
can not > 30, now 33
fib(7) result is 13.
fib(12) result is 144.
fib(8) result is 21.
fib(16) result is 987.

'''

等价写法:

with ProcessPoolExecutor(max_workers=3) as executor:
    futures = {}
    for n in nums:
      job = executor.submit(fib, n)
      futures[job] = n

    for job in as_completed(futures):
      try:
        re = job.result()
        n = futures[job]
        print('fib(%s) result is %s.' % (n, re))
      except Exception as e:
        print(e)
'''
fib(13) result is 233.
fib(17) result is 1597.
fib(0) result is 1.
fib(22) result is 17711.
can not > 30, now 33
fib(7) result is 13.
fib(19) result is 4181.
fib(8) result is 21.
fib(12) result is 144.
fib(16) result is 987.
'''

方案二:map

map 输出结果按照输入数组的顺序

缺点:某一子进程异常会导致整体中断

with ProcessPoolExecutor(max_workers=3) as executor:
    try:
      results = executor.map(fib, nums)
      for num, result in zip(nums, results):
        print('fib(%s) result is %s.' % (num, result))
    except Exception as e:
      print(e)
'''
fib(13) result is 233.
fib(17) result is 1597.
fib(0) result is 1.
fib(22) result is 17711.
fib(19) result is 4181.
can not > 30, now 33
'''

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用百度API上传文件到百度网盘代码分享
Nov 08 Python
Python导入模块时遇到的错误分析
Aug 30 Python
《Python学习手册》学习总结
Jan 17 Python
Python访问MongoDB,并且转换成Dataframe的方法
Oct 15 Python
python无限生成不重复(字母,数字,字符)组合的方法
Dec 04 Python
python通过ffmgep从视频中抽帧的方法
Dec 05 Python
Python TestCase中的断言方法介绍
May 02 Python
python将图片转base64,实现前端显示
Jan 09 Python
利用Python中的Xpath实现一个在线汇率转换器
Sep 09 Python
如何一键升级Python所有包
Nov 05 Python
Jupyter Notebook添加代码自动补全功能的实现
Jan 07 Python
python爬虫如何解决图片验证码
Feb 14 Python
Python 中list ,set,dict的大规模查找效率对比详解
Oct 11 #Python
Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】
Oct 11 #Python
Python 进程操作之进程间通过队列共享数据,队列Queue简单示例
Oct 11 #Python
Python进程,多进程,获取进程id,给子进程传递参数操作示例
Oct 11 #Python
Python中的延迟绑定原理详解
Oct 11 #Python
python pycharm的安装及其使用
Oct 11 #Python
详解Python3迁移接口变化采坑记
Oct 11 #Python
You might like
php 生成随机验证码图片代码
2010/02/08 PHP
PHP生成静态HTML页面最简单方法示例
2015/04/09 PHP
php实现统计网站在线人数的方法
2015/05/12 PHP
PHP+AJAX实现投票功能的方法
2015/09/28 PHP
深入浅析用PHP实现MVC
2016/03/02 PHP
PHP会话控制实例分析
2016/12/24 PHP
jQuery bxCarousel实现图片滚动切换效果示例代码
2013/05/15 Javascript
js如何取消事件冒泡
2013/09/23 Javascript
深入理解Javascript里的依赖注入
2014/03/19 Javascript
javascript 中that的含义示例介绍
2014/05/14 Javascript
基于JS代码实现导航条弹出式悬浮菜单
2016/06/17 Javascript
灵活使用数组制作图片切换js实现
2016/07/28 Javascript
React创建组件的三种方式及其区别
2017/01/12 Javascript
js实现按座位号抽奖
2017/04/05 Javascript
javascript完美实现给定日期返回上月日期的方法
2017/06/15 Javascript
基于JavaScript实现新增内容滚动播放效果附完整代码
2017/08/24 Javascript
详解Vue 中 extend 、component 、mixins 、extends 的区别
2017/12/20 Javascript
vue中使用echarts制作圆环图的实例代码
2018/07/27 Javascript
vue远程加载sfc组件思路详解
2019/12/25 Javascript
原生js实现随机点名
2020/07/05 Javascript
python使用BeautifulSoup分页网页中超链接的方法
2015/04/04 Python
利用matplotlib+numpy绘制多种绘图的方法实例
2017/05/03 Python
Win7下Python与Tensorflow-CPU版开发环境的安装与配置过程
2018/01/04 Python
Python变量赋值的秘密分享
2018/04/03 Python
Python实现爬虫设置代理IP和伪装成浏览器的方法分享
2018/05/07 Python
Python中使用Selenium环境安装的方法步骤
2021/02/22 Python
html5拖曳操作 HTML5实现网页元素的拖放操作
2013/01/02 HTML / CSS
一些网络技术方面的面试题
2014/05/01 面试题
公司出纳岗位职责
2013/12/07 职场文书
销售员岗位职责
2014/06/09 职场文书
开展党的群众路线教育实践活动工作总结
2014/11/05 职场文书
党员志愿者服务倡议书
2015/04/29 职场文书
2016年小学生新年寄语
2015/08/18 职场文书
小学语文教师研修感悟
2015/11/18 职场文书
工伤调解协议书
2016/03/21 职场文书
Python中else的三种使用场景
2021/06/16 Python