Writing a parallel loop

后端 未结 2 528
清歌不尽
清歌不尽 2021-02-13 07:16

I am trying to run a parallel loop on a simple example.
What am I doing wrong?

from joblib import Parallel, delayed  
import multiprocessing

def processInpu         


        
2条回答
  •  别那么骄傲
    2021-02-13 07:24

    Continuing on your request to provide a working multiprocessing code, I suggest that you use pool_map (if the delayed functionality is not important), I'll give you an example, if your'e working on python3 its worth to mention you can use starmap. Also worth mentioning that you can use map_sync/starmap_async if the order of the returned results does not have to correspond to the order of inputs.

    import multiprocessing as mp
    
    def processInput(i):
            return i * i
    
    if __name__ == '__main__':
    
        # what are your inputs, and what operation do you want to
        # perform on each input. For example...
        inputs = range(1000000)
        #  removing processes argument makes the code run on all available cores
        pool = mp.Pool(processes=4)
        results = pool.map(processInput, inputs)
        print(results)
    

提交回复
热议问题