Search code examples
pythonprocessparallel-processingmultiprocessingpool

multiprocessing in python does not stop running


I tried a simple example of multiprocessing in python from their website itself, but it does not give any input. It's showing as running itself and I am not able to stop it in jupyter notebook.

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

It's the same for other multiprocessing examples too. It does not give any error or timeout or anything. It's like it is in an infinite loop or deadlock.


Solution

  • I'm also on Windows.

    1. As pointed out 'from multiprocessing.pool import ThreadPool as Pool' works.
    2. also you need to add if name == 'main':
    3. also below that you need to add : 'with Pool(4) as pool:'

    example

    from multiprocessing.pool import ThreadPool as Pool
    from os import getpid
    import time
    import pandas as pd
    
    
    pyfiles = [10,2,3,5]    
    
    def scraper(x):
        results_df = pd.DataFrame({})
        print('Program started:',x,"I'm process", getpid())
        time.sleep(x)
        print('Program completed:',x)
        results_df.to_csv('multi{}.csv'.format(x))
    
    
    if __name__ == '__main__':
        with Pool(4) as pool:
            start=time.time()
            result = pool.map(scraper, pyfiles)
            pool.terminate()
            pool.join()
            print("Time Taken: ",str(time.time()-start))