简体   繁体   English

如何在 Multiprocessing executor.map() function 中传递多个 arguments

[英]How to pass multiple arguments in Multiprocessing executor.map() function

I have been watching several videos on Multiprocessing map function.我一直在观看有关多处理map function 的几个视频。

I know that I can send one list as an argument to the function I want to target with Multiprocessing, and that will call the same function n times (dependent upon the size of that passed list).我知道我可以将一个列表作为参数发送到我想要使用多处理的 function,这将调用相同的 function n 次(取决于传递的列表的大小)。

What I am struggling to do is what if I want to pass multiple arguments to that function?我正在努力做的是,如果我想将多个 arguments 传递给那个 function 怎么办?

I basically have a List whose size is n (it can vary but for current case, its 209)我基本上有一个大小为n的列表(它可以变化,但对于当前情况,它是 209)

My function requires 3 arguments...我的 function 需要 3 arguments...

  1. the index of the list (0, 1, 2 etc.)列表的索引(0、1、2 等)
  2. Another list containing data另一个包含数据的列表
  3. A fixed integer value一个固定的 integer 值

I could have used the 2nd and 3rd arguments as global variables, but that doesn't work for me because I have to call the map function in a while loop... and in every another iteration, the values of these two will change.我本可以使用第二个和第三个 arguments 作为全局变量,但这对我不起作用,因为我必须在 while 循环中调用 map function ...并且在每一次迭代中,这两个的值都会改变。

My function returns two values which I need to access in the function from where it was called.我的 function返回两个值,我需要从调用它的地方访问 function。 This is what I have tried but it didn't work for me,这是我尝试过的方法,但对我不起作用,

def main_fun():
    with concurrent.futures.ProcessPoolExecutor() as executor: 

        results = executor.map(MyFun, (row, pop[0].data, fitness) for row in range(0, len(pop[0].data)))

        for result in results:
            print(result)

I also tried to use ZIP function but again, with no success.我也尝试使用ZIP function 但再次没有成功。

If your second and third arguments to your worker function (ie the first argument to map ), then you can use method functools.partial to have the second and third arguments specified without resorting to the use of global variables.如果您的第二个和第三个 arguments 给您的工作人员 function(即第一个参数map ),那么您可以使用方法functools.partial指定第二个和第三个 arguments,而无需使用全局变量。 If your worker functions is, for example, foo , then:例如,如果您的辅助函数是foo ,那么:

from concurrent.futures import ProcessPoolExecutor
from functools import partial


def foo(idx: int, lst: list, int_value: int):
    ...

def main():
    with ProcessPoolExecutor() as executor:
        worker = partial(foo, lst=pop[0].data, int_value=fitness)
        executor.map(worker, range(0, len(pop[0].data)))

if __name__ == '__main__':
    main()

So now we only have to pass to map function worker , which will be called two fixed arguments, and a single iterable argument.所以现在我们只需要传递给map function worker ,这将被称为两个固定的 arguments 和一个可迭代的参数。

If you are executing the map call in a loop, you will, of course, create a new worker functions by passing to functools.partial new arguments.如果您在循环中执行map调用,您当然会通过传递给functools.partial new arguments 创建一个新的worker函数。

For example:例如:

from concurrent.futures import ProcessPoolExecutor
from functools import partial


def foo(idx: int, lst: list, int_value: int):
    print(idx, lst[idx] * int_value, flush=True)

def main():
    l = [3, 5, 7]
    fitness = 9
    with ProcessPoolExecutor() as executor:
        worker = partial(foo, lst=l, int_value=fitness)
        executor.map(worker, range(0, len(l)))

if __name__ == '__main__':
    main()

Prints:印刷:

0 27
1 45
2 63

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中对关键字参数使用executor.map函数 - How to use executor.map function in python on keyword arguments 如何正确地将生成器传递给 ThreadPoolExecutor Executor.map() 的 function 参数? - How to correctly pass the generator to the function parameters of ThreadPoolExecutor Executor.map()? 如何将多个参数传递给由 concurrent.futures.ProcessPoolExecutor 中的 executor.map() 迭代的函数 - How to pass several parameters to a function which is iterated by executor.map() from concurrent.futures.ProcessPoolExecutor Python 如何在执行程序中传递参数对列表。map - Python how to pass list of argument pair in executor.map 如何在没有函数的情况下将executor.map应用于for循环? - How to apply executor.map to for loop without a function? 为什么来自多处理的 executor.map 再次运行整个主程序? - Why is executor.map from multiprocessing running the entire main again? 如何将executor.map()置于循环中并分离每个迭代? - How to put executor.map() in a loop and separate each iteration? 如何从 executor.map() 创建的生成器 object 中提取结果 - How to extract result from a generator object that created by executor.map() Python 并发 executor.map() 和 submit() - Python concurrent executor.map() and submit() executor.map和非终止参数 - executor.map and non-terating parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM