How to use multiprocessing pool.map with multiple arguments?

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

    text = "test"
    
    def unpack(args):
        return args[0](*args[1:])
    
    def harvester(text, case):
        X = case[0]
        text+ str(X)
    
    if __name__ == '__main__':
        pool = multiprocessing.Pool(processes=6)
        case = RAW_DATASET
        # args is a list of tuples 
        # with the function to execute as the first item in each tuple
        args = [(harvester, text, c) for c in case]
        # doing it this way, we can pass any function
        # and we don't need to define a wrapper for each different function
        # if we need to use more than one
        pool.map(unpack, args)
        pool.close()
        pool.join()
    

提交回复
热议问题