How to use multiprocessing pool.map with multiple arguments?

前端 未结 20 3479
-上瘾入骨i
-上瘾入骨i 2020-11-21 11:24

In the Python multiprocessing library, is there a variant of pool.map which supports multiple arguments?

text = "test"
def         


        
20条回答
  •  再見小時候
    2020-11-21 12:03

    You can use the following two functions so as to avoid writing a wrapper for each new function:

    import itertools
    from multiprocessing import Pool
    
    def universal_worker(input_pair):
        function, args = input_pair
        return function(*args)
    
    def pool_args(function, *args):
        return zip(itertools.repeat(function), zip(*args))
    

    Use the function function with the lists of arguments arg_0, arg_1 and arg_2 as follows:

    pool = Pool(n_core)
    list_model = pool.map(universal_worker, pool_args(function, arg_0, arg_1, arg_2)
    pool.close()
    pool.join()
    

提交回复
热议问题