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中的多线程编程
Apr 09 Python
在Django的URLconf中使用命名组的方法
Jul 18 Python
Python中表示字符串的三种方法
Sep 06 Python
浅谈Python2获取中文文件名的编码问题
Jan 09 Python
python中将字典形式的数据循环插入Excel
Jan 16 Python
python如何对实例属性进行类型检查
Mar 20 Python
Python 实现「食行生鲜」签到领积分功能
Sep 26 Python
在matplotlib的图中设置中文标签的方法
Dec 13 Python
使用python opencv对目录下图片进行去重的方法
Jan 12 Python
Python3 使用map()批量的转换数据类型,如str转float的实现
Nov 29 Python
python小程序基于Jupyter实现天气查询的方法
Mar 27 Python
python中K-means算法基础知识点
Jan 25 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实现像JSP,ASP里Application那样的全局变量
2007/01/12 PHP
PHP ajax 分页类代码
2008/11/13 PHP
详解js异步文件加载器
2016/01/24 PHP
php基于ob_start(ob_gzhandler)实现网页压缩功能的方法
2017/02/18 PHP
PHP设计模式(一)工厂模式Factory实例详解【创建型】
2020/05/02 PHP
对联广告js flash激活
2006/10/19 Javascript
基于jquery的9行js轻松实现tab控件示例
2013/10/12 Javascript
jQuery中width()方法用法实例
2014/12/24 Javascript
javascript发送短信验证码实现代码
2015/11/12 Javascript
JavaScript接口的实现三种方式(推荐)
2016/06/14 Javascript
用vue和node写的简易购物车实现
2017/04/25 Javascript
Angular.js中上传指令ng-upload的基本使用教程
2017/07/30 Javascript
React Native实现地址挑选器功能
2017/10/24 Javascript
基于vue配置axios的方法步骤
2017/11/09 Javascript
vue初始化动画加载的实例
2018/09/01 Javascript
python获取标准北京时间的方法
2015/03/24 Python
Python面向对象编程中的类和对象学习教程
2015/03/30 Python
学习python类方法与对象方法
2016/03/15 Python
利用Python实现颜色色值转换的小工具
2016/10/27 Python
Python判断文件和字符串编码类型的实例
2017/12/21 Python
Python切片工具pillow用法示例
2018/03/30 Python
python 把文件中的每一行以数组的元素放入数组中的方法
2018/04/29 Python
python3.6使用urllib完成下载的实例
2018/12/19 Python
详解Python3中ceil()函数用法
2019/02/19 Python
在PyCharm的 Terminal(终端)切换Python版本的方法
2019/08/02 Python
Python实现查找数据库最接近的数据
2020/06/08 Python
详解python方法之绑定方法与非绑定方法
2020/08/17 Python
日本7net购物网:书籍、漫画、杂志、DVD、游戏邮购
2017/02/17 全球购物
随机分配座位,共50个学生,使学号相邻的同学座位不能相邻
2014/01/18 面试题
挂职思想汇报
2013/12/31 职场文书
公司年会演讲稿范文
2014/01/11 职场文书
中文专业学生自我评价范文
2014/02/06 职场文书
简易离婚协议书(范本)
2014/10/25 职场文书
解除同居协议书
2015/01/29 职场文书
深度好文:50条没人告诉你的人生经验,句句精辟
2019/08/22 职场文书
「地球外少年少女」BD发售宣传CM公开
2022/03/21 日漫