How to use multiprocessing pool.map with multiple arguments?

前端 未结 20 3476
-上瘾入骨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 11:48

    Another way is to pass a list of lists to a one-argument routine:

    import os
    from multiprocessing import Pool
    
    def task(args):
        print "PID =", os.getpid(), ", arg1 =", args[0], ", arg2 =", args[1]
    
    pool = Pool()
    
    pool.map(task, [
            [1,2],
            [3,4],
            [5,6],
            [7,8]
        ])
    

    One can than construct a list lists of arguments with one's favorite method.

提交回复
热议问题