简体   繁体   中英

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.

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))

I dont know how but it worked when i imported like given below.

from multiprocessing.pool import ThreadPool as Pool

The problem was with pool in importing multiprocessing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM