9

I am learning about multiprocessing in python. I have the following code snippet:

import time
import concurrent.futures

def wait(seconds):
    print(f'Waiting {seconds} seconds...')
    time.sleep(seconds)
    return f'Done'

if __name__ == "__main__":
    with concurrent.futures.ProcessPoolExecutor() as executor:
        p = executor.submit(wait,1)
        print(p.result())

Running it gives me this error:

BrokenProcessPool                         Traceback (most recent call last)
<ipython-input-19-8eff57e3a077> in <module>
      9     with concurrent.futures.ProcessPoolExecutor() as executor:
     10         p = executor.submit(do_something,1)
---> 11         print(p.result())

~\.conda\envs\w\lib\concurrent\futures\_base.py in result(self, timeout)
    433                 raise CancelledError()
    434             elif self._state == FINISHED:
--> 435                 return self.__get_result()
    436             else:
    437                 raise TimeoutError()

~\.conda\envs\w\lib\concurrent\futures\_base.py in __get_result(self)
    382     def __get_result(self):
    383         if self._exception:
--> 384             raise self._exception
    385         else:
    386             return self._result

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

I am on a Windows computer and I have used if __name__ == "__main__": in my code. But I still get this error.

  • I referred all these SO posts but didn't find a solution: https://stackoverflow.com/questions/44923476/concurrent-futures-code-ends-up-with-brokenprocesspool https://stackoverflow.com/questions/59581158/multiprocessing-help-brokenprocesspool-error https://stackoverflow.com/questions/50611414/jupyter-notebook-always-gives-brokenprocesspool-error-while-executing-qiskit-cod – Sabito stands with Ukraine Jun 20 '20 at 16:18
  • did you try restarting the nb and running the code again? – YOLO Jun 20 '20 at 16:22
  • @YOLO yes I did it but still I am getting this error – Sabito stands with Ukraine Jun 20 '20 at 17:12

1 Answers1

9

I got it to work! I saved the wait function in a separate python file called wait.py and imported it in jupyter notebook.

wait.py:

import time

def wait(seconds):
    print(f'Waiting {seconds} seconds...')
    time.sleep(seconds)
    return f'Done'

ipynb file:

import concurrent.futures
import wait #import the wait file

if __name__ == "__main__":
    with concurrent.futures.ProcessPoolExecutor() as executor:
        p = executor.submit(wait.wait,1)
        print(p.result())