How to use multiprocessing pool.map with multiple arguments?

前端 未结 20 3497
-上瘾入骨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:52

    Using Python 3.3+ with pool.starmap():

    from multiprocessing.dummy import Pool as ThreadPool 
    
    def write(i, x):
        print(i, "---", x)
    
    a = ["1","2","3"]
    b = ["4","5","6"] 
    
    pool = ThreadPool(2)
    pool.starmap(write, zip(a,b)) 
    pool.close() 
    pool.join()
    

    Result:

    1 --- 4
    2 --- 5
    3 --- 6
    

    You can also zip() more arguments if you like: zip(a,b,c,d,e)

    In case you want to have a constant value passed as an argument you have to use import itertools and then zip(itertools.repeat(constant), a) for example.

提交回复
热议问题