使用Joblib进行多处理:对函数的一个参数进行并行化

2024-10-02 04:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法让parallel函数接受多个参数,但只在其中一个参数上并行

假设我有一些代码:

def my_function(graph,graph_coefficients, thing_i_want_to_parallelise_over):

     <do_thing>

     return value 


results = Parallel(n_job2=2)(delayed(my_function(one_graph,graph_coefficients)(thing_i_want_to_parallelise_over) for thing_i_want_to_parallelise_over in range(1,3))

有办法做到这一点吗?有多个函数可以调用,因此执行一个简单的概括函数实际上不是一个选项


Tags: to函数代码参数parallelmydeffunction
1条回答
网友
1楼 · 发布于 2024-10-02 04:17:14

我不知道我是否理解您的问题,但您的格式不正确

您应该创建包含所有参数的元组

(one_graph, graph_coefficients, x) for x in range(1,3)   # args

然后你就应该把它和

delayed( my_function )

results = Parallel(n_jobs=2)( 
                delayed( my_function )
                (one_graph, graph_coefficients, x) for x in range(1,3)
          )

最终,您可以尝试使用lambda

lambda x: my_function(one_graph, graph_coefficients,x)

然后你可以使用

(x) for x in range(1,3)

results = Parallel(n_jobs=2)(
                delayed( lambda x: my_function(one_graph, graph_coefficients,x) ) 
                (x) for x in range(1,3) 
          )

或与functools.partial

partial(my_function, one_graph, graph_coefficients)

from functools import partial

results = Parallel(n_jobs=2)(
                delayed( partial(my_function, one_graph, graph_coefficients) ) 
                (x) for x in range(1,3) 
          )

最小工作代码

from joblib import Parallel, delayed

def my_function(graph, graph_coefficients, thing_i_want_to_parallelise_over):
    print('my_function:', graph, graph_coefficients, thing_i_want_to_parallelise_over)
    value = 2 * thing_i_want_to_parallelise_over
    return value 

one_graph = 'A'
graph_coefficients = 'B'

#   

results = Parallel(n_jobs=2)(
                delayed( my_function ) 
                (one_graph, graph_coefficients, x) for x in range(1,3) 
          )

print('results:', results)

#   

results = Parallel(n_jobs=2)(
                delayed( lambda x: my_function(one_graph, graph_coefficients,x) ) 
                (x) for x in range(1,3) 
          )

print('results:', results)

#   

from functools import partial

results = Parallel(n_jobs=2)(
                delayed( partial(my_function, one_graph, graph_coefficients) ) 
                (x) for x in range(1,3) 
          )

print('results:', results)

相关问题 更多 >

    热门问题