【发布时间】:2020-05-18 00:54:21
【问题描述】:
我在 MacBook 上的 IPython 解释器(IPython 7.9.0、Python 3.8.0)中运行了一个简单的多处理示例,但遇到了一个奇怪的错误。这是我输入的内容:
[In [1]: from concurrent.futures import ProcessPoolExecutor
[In [2]: executor=ProcessPoolExecutor(max_workers=1)
[In [3]: def func():
print('Hello')
[In [4]: future=executor.submit(func)
但是,我收到以下错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 313, in _bootstrap
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/concurrent/futures/process.py", line 233, in _process_worker
call_item = call_queue.get(block=True)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/queues.py", line 116, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'func' on <module '__main__' (built-in)>
此外,再次尝试提交作业给了我一个不同的错误:
[In [5]: future=executor.submit(func)
---------------------------------------------------------------------------
BrokenProcessPool Traceback (most recent call last)
<ipython-input-5-42bad1a6fe80> in <module>
----> 1 future=executor.submit(func)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/concurrent/futures/process.py in submit(*args, **kwargs)
627 with self._shutdown_lock:
628 if self._broken:
--> 629 raise BrokenProcessPool(self._broken)
630 if self._shutdown_thread:
631 raise RuntimeError('cannot schedule new futures after shutdown')
BrokenProcessPool: A child process terminated abruptly, the process pool is not usable anymore
作为完整性检查,我将相同的(几乎)代码输入到 Python 文件中,并从命令行 (python3 test.py) 运行它。效果很好。
为什么 IPython 对我的测试有问题?
编辑:
这是运行良好的 Python 文件。
from concurrent.futures import ProcessPoolExecutor as Executor
def func():
print('Hello')
if __name__ == '__main__':
with Executor(1) as executor:
future=executor.submit(func)
print(future.result())
【问题讨论】:
-
你的环境是什么?我在 Ubuntu 上的 ipython (7.14) 中运行了你的代码,它运行良好。我知道与 Windows 相关的多处理问题,但我没有要测试的 Windows 机器。如果您在 Windows 上运行,请将其添加到问题中,因为它可能是相关的。
-
我在 MacBook 上运行。我已将其添加到 OP 中。
-
我刚刚将 iPython 升级到 7.14 并再次运行它。同样的错误。
标签: python multiprocessing ipython