gpt4 book ai didi

python - 在IPython中运行ProcessPoolExecutor

转载 作者:行者123 更新时间:2023-12-03 13:45:35 29 4
gpt4 key购买 nike

我在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())

最佳答案

好的,终于找到了正在发生的事情。问题是Mac OS-默认情况下,它使用“spawn”方法创建子进程。这在https://docs.python.org/3/library/multiprocessing.html中进行了说明,以及将其更改为fork的方法(尽管它指出fork在Mac os上是不安全的)。

使用spawn方法,将启动一个新的Python解释器,并将您的代码提供给它。然后,它尝试将函数定位在main下,但是在这种情况下,因为没有程序,只有解释的命令,所以没有main。

如果将start方法更改为fork,则代码将运行(但请注意有关此操作不安全的警告)

In [1]: import multiprocessing as mp                                                                                     

In [2]: mp.set_start_method("fork")

In [3]: def func():
...: print("foo");
...:

In [4]: from concurrent.futures import ProcessPoolExecutor

In [5]: executor=ProcessPoolExecutor(max_workers=1)

In [6]: future=executor.submit(func)

foo
In [7]:

由于警告,我不确定答案是否有用,但是它解释了为什么当您有程序(其他尝试)时它的行为会有所不同,以及为什么它在Ubuntu上能正常工作-默认情况下使用“fork”。

关于python - 在IPython中运行ProcessPoolExecutor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61860800/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com