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

Python 使用Joblib进行多处理:对函数的一个参数进行并行化,python,multiprocessing,joblib,Python,Multiprocessing,Joblib,有没有一种方法可以让parallel函数接受多个参数,但只在其中一个参数上并行 假设我有一些代码: def my_function(graph,graph_coefficients, thing_i_want_to_parallelise_over): <do_thing> return value results = Parrallel(n_job2=2)(delayed(my_function(one_graph,graph_coefficients

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

假设我有一些代码:

def my_function(graph,graph_coefficients, thing_i_want_to_parallelise_over):

     <do_thing>

     return value 


results = Parrallel(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))

def my_函数(图形、图形系数、我希望并行化的对象):
返回值
结果=Parralel(n_job2=2)(延迟(我的函数(一个图,图系数)(我希望我的东西并行化)用于范围(1,3)内的东西并行化)

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

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

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

(one_graph, graph_coefficients, x) for x in range(1,3)   # args
然后你就应该把它和

delayed( my_function )


最终,您可以尝试使用
lambda

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 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)

可能首先用
[(一个图,一个图,一个系数,1),(一个图,一个系数,2)]
创建列表,然后在
parralel
中使用它,我添加了
functools.partial
而不是
lambda
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)