3
votes

I'm trying to learn the basics of multi-processing in python, and found the following example online which I wanted to practice with.

import concurrent.futures
import time

def do_something(seconds):    
    print(f' Sleeping {seconds} seconds')
    time.sleep(seconds)
    return f'Done Sleeping {seconds}'

with concurrent.futures.ProcessPoolExecutor() as executor:
    f1 = executor.submit(do_something, 1)
    print(f1.result())

Fairly simple, I know. However, for some reason when I try and run this, I get the following error.

Traceback (most recent call last):

File "", line 19, in print(f1.result())

File "C:\Anaconda3\lib\concurrent\futures_base.py", line 432, in result return self.__get_result()

File "C:\Anaconda3\lib\concurrent\futures_base.py", line 384, in __get_result raise self._exception

BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

Any idea what is causing this?

1
I can only imagine that print(f1.result()) raised an exception, e.g. because f1 does not have a .result of right type, or the call to .result() crashed. - 9000

1 Answers

-1
votes

You might want to check if the future is done already:

if f1.done():
    print(f1.result())