简体   繁体   English

在 jupyter notebook 中运行代码时出现 BrokenProcessPool

[英]BrokenProcessPool while running code in jupyter notebook

I am learning about multiprocessing in python.我正在学习 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.我在 Windows 计算机上,并且在我的代码中使用了if __name__ == "__main__": But I still get this error.但我仍然得到这个错误。

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 function 保存在名为wait.py的单独 python 文件中,并将其导入 jupyter 笔记本。

wait.py : wait.py

import time

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

ipynb file : ipynb file

import concurrent.futures
import wait

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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