Reputation: 115
I am trying to run this following code on Jupyter notebook (using Python 3.7.5) :
import time
import concurrent.futures
start = time.perf_counter()
def do_something(seconds):
print(f'sleeping for {seconds} seconds')
time.sleep(seconds)
return 'done sleeping'
with concurrent.futures.ProcessPoolExecutor() as executor:
results = [executor.submit(do_something, 1) for _ in range(10)]
for f in concurrent.futures.as_completed(results):
print(f.result())
finish = time.perf_counter()
print(f'finished in {round(finish-start,9)} seconds')
But, the interpreter shows an error message :
BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
I am learning multiprocessing from an YouTube tutorial where this code works fine. I haven't found any webpage discussing this error.
Upvotes: 1
Views: 671
Reputation: 8576
The code is correct; however it will not work in Jupyter.
Its expected behaviour according to the docs:
The main module must be importable by worker subprocesses. This means that ProcessPoolExecutor will not work in the interactive interpreter.
Upvotes: 4