【问题标题】:Run Python Multiprocessing of same function with multiple parameters使用多个参数运行相同函数的 Python 多处理
【发布时间】:2021-05-08 00:53:57
【问题描述】:

我有一个函数说:

 Fun1(a,b):
   return a*b

我想用不同的 a 和 b 值多次调用 Fun1。 需要使用多处理,因为 Fun1 是一个称为数百万次的

【问题讨论】:

  • 你检查过ProcessPollExecutormap 吗?
  • 你能给我举个例子吗?似乎无法为函数使用 2 个不同的参数

标签: python python-2.7 multiprocessing


【解决方案1】:
from concurrent.futures import ProcessPoolExecutor


def fun1(a, b):
    return a * b

# if __name__ == '__main__': is required for platforms that do not
# support fork(), such as Windows:
if __name__ == '__main__':
    a = [1, 2, 3]
    b = [4, 5, 6]
    with ProcessPoolExecutor(max_workers = 3) as executor:
        results = executor.map(fun1, a, b)
    for result in results:
        print(result)
4
10
18

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2019-11-29
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2018-10-17
    • 1970-01-01
    相关资源
    最近更新 更多