如何在Python中并行化以下搜索?

问题描述 投票:0回答:2

我想并行化以下最佳参数搜索(我在 MacBook Air(Apple M2 24Go)上的 jupyter 实验室工作)

def search(list):
    result=[0,0,0,0]
    for (a,b,c) in list:
         p= calcul(a,b,c)
         if p> result[0]:
             result=[p,a,b,c]
     return result

calcul 是一个取正值的辅助函数。 提前谢谢!

python parallel-processing
2个回答
0
投票

您可以使用 multiprocessing 模块并行计算中间值,然后找到最大值

import multiprocessing as mp

def mapper(args):
    a, b, c = args
    return [calcul(a, b, c), a, b, c]

def search(list):
    with mp.Pool(processes = mp.cpu_count()) as pool:
        mapped = pool.map(mapper, list)
        return max(mapped, key=lambda lst: lst[0])

0
投票

您可以使用 multiprocessing.Pool 并行运行计算

import random
from multiprocessing import Pool

def calcul(Tuple):
    a,b,c = Tuple
    return a+b+c

def search():
    # demo data
    l = [(random.randint(0,10),random.randint(0,10),random.randint(0,10)) for _ in range(10)]
    print(l)
    
    with Pool() as pool: # parallel processing
        Result = pool.map(calcul,l)
    print(max(Result)) # maximum of the results

if __name__=="__main__": # run in this guard to not cause other issues
    search()
© www.soinside.com 2019 - 2024. All rights reserved.