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进程类subprocess的一些操作方法例子
Nov 22 Python
Python fileinput模块使用介绍
Nov 30 Python
Python编程对列表中字典元素进行排序的方法详解
May 26 Python
matplotlib绘制符合论文要求的图片实例(必看篇)
Jun 02 Python
python高级特性和高阶函数及使用详解
Oct 17 Python
解决python3中cv2读取中文路径的问题
Dec 05 Python
python实现BP神经网络回归预测模型
Aug 09 Python
python爬虫 2019中国好声音评论爬取过程解析
Aug 26 Python
Python3实现配置文件差异对比脚本
Nov 18 Python
python virtualenv虚拟环境配置与使用教程详解
Jul 13 Python
pytorch加载预训练模型与自己模型不匹配的解决方案
May 13 Python
python开发飞机大战游戏
Jul 15 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使用Imagick生成图片的方法
2015/07/31 PHP
PHP面试题之文件目录操作
2015/10/15 PHP
php 在字符串指定位置插入新字符的简单实现
2016/06/28 PHP
JQuery select控件的相关操作实现代码
2012/09/14 Javascript
javascript实现移动端上的触屏拖拽功能
2016/03/04 Javascript
prototype与__proto__区别详细介绍
2017/01/09 Javascript
html5+canvas实现支持触屏的签名插件教程
2017/05/08 Javascript
vue上传图片组件编写代码
2017/07/26 Javascript
使用原生js+canvas实现模拟心电图的实例
2017/09/20 Javascript
JS计算输出100元钱买100只鸡问题的解决方法
2018/01/04 Javascript
jQuery中实现text()的方法
2019/04/04 jQuery
layui button 按钮弹出提示窗口,确定才进行的方法
2019/09/06 Javascript
toString.call()通用的判断数据类型方法示例
2020/08/28 Javascript
Python实现把utf-8格式的文件转换成gbk格式的文件
2015/01/22 Python
在RedHat系Linux上部署Python的Celery框架的教程
2015/04/07 Python
梯度下降法介绍及利用Python实现的方法示例
2017/07/12 Python
Python实现调用另一个路径下py文件中的函数方法总结
2018/06/07 Python
Python GUI布局尺寸适配方法
2018/10/11 Python
Python字符串对象实现原理详解
2019/07/01 Python
python查看数据类型的方法
2019/10/12 Python
python进程间通信Queue工作过程详解
2019/11/01 Python
Win10里python3创建虚拟环境的步骤
2020/01/31 Python
谈谈Python:为什么类中的私有属性可以在外部赋值并访问
2020/03/05 Python
django实现将修改好的新模型写入数据库
2020/03/31 Python
Django中使用Json返回数据的实现方法
2020/06/03 Python
Python读写压缩文件的方法
2020/07/30 Python
女性时尚网购:Chic Me
2019/07/30 全球购物
集体婚礼证婚词
2014/01/13 职场文书
检查接待方案
2014/02/27 职场文书
网络优化专员求职信
2014/05/04 职场文书
艺术设计专业毕业生推荐信
2014/07/08 职场文书
中学生打架检讨书
2014/10/13 职场文书
2015年纪检监察工作总结
2015/04/08 职场文书
红高粱观后感
2015/06/10 职场文书
职业规划从高考志愿专业选择开始
2019/08/08 职场文书
PHP 技巧 * SVG 保存为图片(分享图生成)
2021/04/02 PHP