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中使用logging模块打印log日志详解
Apr 05 Python
Python和C/C++交互的几种方法总结
May 11 Python
Sublime开发python程序的示例代码
Jan 24 Python
详解Python匿名函数(lambda函数)
Apr 19 Python
python实现文件的备份流程详解
Jun 18 Python
Python Web框架之Django框架Model基础详解
Aug 16 Python
多版本python的pip 升级后, pip2 pip3 与python版本失配解决方法
Sep 11 Python
Python 装饰器@,对函数进行功能扩展操作示例【开闭原则】
Oct 17 Python
TFRecord格式存储数据与队列读取实例
Jan 21 Python
基于keras输出中间层结果的2种实现方式
Jan 24 Python
你需要学会的8个Python列表技巧
Jun 24 Python
教你利用Selenium+python自动化来解决pip使用异常
May 20 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
用PHP4访问Oracle815
2006/10/09 PHP
php 在windows下配置虚拟目录的方法介绍
2013/06/26 PHP
PHP文件操作方法汇总
2015/07/01 PHP
PHP 极验验证码实例讲解
2016/09/29 PHP
JavaScript 利用StringBuffer类提升+=拼接字符串效率
2009/11/24 Javascript
javascript 命名规则 变量命名规则
2010/02/25 Javascript
Jquery拖拽并简单保存的实现代码
2010/11/28 Javascript
js 文本滚动效果的实例代码
2013/08/17 Javascript
nodejs教程之入门
2014/11/21 NodeJs
JavaScript判断图片是否已经加载完毕的方法汇总
2016/02/05 Javascript
javascript中call,apply,bind函数用法示例
2016/12/19 Javascript
微信小程序调用摄像头隐藏式拍照功能
2018/08/22 Javascript
vue中实现Monaco Editor自定义提示功能
2019/07/05 Javascript
JavaScript快速调试的两个技巧
2020/11/04 Javascript
解决Vue-cli3没有vue.config.js文件夹及配置vue项目域名的问题
2020/12/04 Vue.js
JS实现选项卡插件的两种写法(jQuery和class)
2020/12/30 jQuery
[01:14:55]EG vs Spirit Supermajor 败者组 BO3 第三场 6.4
2018/06/05 DOTA
pandas修改DataFrame列名的方法
2018/04/08 Python
对python 多个分隔符split 的实例详解
2018/12/20 Python
windows下搭建python scrapy爬虫框架步骤
2018/12/23 Python
python 用户交互输入input的4种用法详解
2019/09/24 Python
详解opencv中画圆circle函数和椭圆ellipse函数
2019/12/27 Python
Tensorflow中tf.ConfigProto()的用法详解
2020/02/06 Python
Python sqlite3查询操作过程解析
2020/02/20 Python
基于Python绘制美观动态圆环图、饼图
2020/06/03 Python
Keras中 ImageDataGenerator函数的参数用法
2020/07/03 Python
python 窃取摄像头照片的实现示例
2021/01/08 Python
canvas如何实现多张图片编辑的图片编辑器
2020/03/10 HTML / CSS
Philosophy美国官网:美国美容品牌
2016/08/15 全球购物
expedia比利时:预订航班+酒店并省钱
2018/07/13 全球购物
生物科学专业个人求职信范文
2013/12/07 职场文书
工作岗位说明书模板
2014/05/09 职场文书
2015年护士节慰问信
2015/03/23 职场文书
《认识年月日》教学反思
2016/02/19 职场文书
python函数指定默认值的实例讲解
2021/03/29 Python
使用CSS实现六边形的图片效果
2022/08/05 HTML / CSS