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

作者:颹蕭蕭 时间:2023-10-23 14:41:56 

使用 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
'''

来源:https://blog.csdn.net/itnerd/article/details/102477783

标签:python,ProcessPoolExecutor,多进程并行
0
投稿

猜你喜欢

  • Pycharm配置opencv与numpy的实现

    2021-09-19 08:20:55
  • 解析SQL Server中数据库快照的工作原理

    2009-02-19 17:04:00
  • Python实现比较两个列表(list)范围

    2022-07-20 18:08:04
  • MySQL数据库事务原理及应用

    2024-01-17 13:02:49
  • Python实现判断并移除列表指定位置元素的方法

    2023-03-21 03:06:19
  • python中的sys模块和os模块

    2021-07-06 00:43:17
  • ASP编程中使用数组方法实例讲解

    2008-02-15 08:25:00
  • Python3.5 Json与pickle实现数据序列化与反序列化操作示例

    2022-04-08 17:53:55
  • Python的Socket编程过程中实现UDP端口复用的实例分享

    2022-07-03 21:59:56
  • python基础while循环及if判断的实例讲解

    2021-02-18 06:56:06
  • vue实际运用之vuex持久化详解

    2024-05-09 15:15:50
  • SQL实现LeetCode(181.员工挣得比经理多)

    2024-01-17 03:15:01
  • pytorch中关于distributedsampler函数的使用

    2023-01-18 01:10:01
  • php读取mysql的简单实例

    2023-11-15 08:57:51
  • oracle命令行删除与创建用户的代码

    2009-03-02 10:54:00
  • 理解SQL SERVER中的逻辑读,预读和物理读

    2012-01-05 19:32:29
  • TensorFlow实现模型断点训练,checkpoint模型载入方式

    2022-01-19 22:32:12
  • Go并发调用的超时处理的方法

    2024-04-28 09:15:40
  • oracle中左填充(lpad)和右填充(rpad)的介绍与用法

    2024-01-28 19:51:19
  • 基于Django的乐观锁与悲观锁解决订单并发问题详解

    2021-07-14 19:42:08
  • asp之家 网络编程 m.aspxhome.com