Python 多处理错误“ForkAwareLocal”对象没有属性“连接”
Posted
技术标签:
【中文标题】Python 多处理错误“ForkAwareLocal”对象没有属性“连接”【英文标题】:Python multiprocessing error 'ForkAwareLocal' object has no attribute 'connection' 【发布时间】:2020-07-02 20:16:49 【问题描述】:下面是我的代码,我正面临一个多处理问题。我看到之前有人问过这个问题,我已经尝试过这些解决方案,但它似乎不起作用。有人可以帮我吗?
from multiprocessing import Pool, Manager
Class X:
def _init_():
def method1(number1,var_a, var_b, var_c, var_d):
return values
if __name__ == 'main':
for value in ["X", "Y"]:
dict_values = Manager().dict()
with Pool(1) as p:
p.starmap(method1,
[
(1, dict_values, var_a, var_b, var_c, var_d),
(2, dict_values, var_a, var_b, var_c, var_d),
(3, var_a, var_b, var_c, var_d)
])
我面临的错误
multiprocessing.pool.RemoteTraceback:
Traceback (most recent call last):
File "/usr/lib64/python3.6/multiprocessing/managers.py", line 749, in _callmethod
conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib64/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/lib64/python3.6/multiprocessing/pool.py", line 47, in starmapstar
return list(itertools.starmap(args[0], args[1]))
File "<string>", line 2, in __setitem__
File "/usr/lib64/python3.6/multiprocessing/managers.py", line 753, in _callmethod
self._connect()
File "/usr/lib64/python3.6/multiprocessing/managers.py", line 740, in _connect
conn = self._Client(self._token.address, authkey=self._authkey)
File "/usr/lib64/python3.6/multiprocessing/connection.py", line 487, in Client
c = SocketClient(address)
File "/usr/lib64/python3.6/multiprocessing/connection.py", line 614, in SocketClient
s.connect(address)
FileNotFoundError: [Errno 2] No such file or directory
【问题讨论】:
【参考方案1】:这个问题的根本原因是主线程完成了两个进程之间的连接。但是,子进程仍在运行,它需要通过队列抛出一些东西。所以让主线程保持活跃是我们的解决方案。
https://jaime-lin.medium.com/python-multiprocessing-manager-error-forkawarelocal-object-has-no-attribute-496009c94aed
【讨论】:
【参考方案2】:也许您正在使用 Pycharm 并在运行/调试配置中选中运行 Python 控制台选项。 尝试取消选中它。
【讨论】:
以上是关于Python 多处理错误“ForkAwareLocal”对象没有属性“连接”的主要内容,如果未能解决你的问题,请参考以下文章