python Pool with worker Processes

后端 未结 3 2041
北恋
北恋 2020-12-04 12:33

I am trying to use a worker Pool in python using Process objects. Each worker (a Process) does some initialization (takes a non-trivial amount of time), gets passed a serie

3条回答
  •  情深已故
    2020-12-04 13:02

    Since python 3.3 you can use starmap, also for using multiple arguments AND getting back the results in a very simplistic syntax:

    import multiprocessing
    
    nb_cores = multiprocessing.cpu_count()
    
    def caps(nb, letter):
        print('Exec nb:', nb)
        return letter.upper()
    
    if __name__ == '__main__':
    
        multiprocessing.freeze_support() # for Windows, also requires to be in the statement: if __name__ == '__main__'
    
        input_data = ['a','b','c','d','e','f','g','h']
        input_order = [1,2,3,4,5,6,7,8,9]
    
        with multiprocessing.Pool(processes=nb_cores) as pool: # auto closing workers
            results = pool.starmap(caps, zip(input_order, input_data))
    
        print(results)
    

提交回复
热议问题