python - 在 multiprocessing.Manager().dict() 上获取 ConnectionRefusedError [Errno 61]
问题描述
长话短说,我试图multiprocessing.Manager().dict()
在主线程中打印 a 的内容,但出现了一个奇怪的错误。
仅当池的数量大于20
(是的,恰好20
)并且仅在macOS
(在 上完美运行Linux
)时,我才遇到此错误。
眼镜:
CPU: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
macOS: 11.2.3
附上最小错误码:
#!/usr/bin/env python3
from contextlib import suppress
import multiprocessing as mp
import time
def run():
D[mp.current_process().name] = 'some val'
time.sleep(0.5)
if __name__ == '__main__':
mp.set_start_method('fork')
D, rets = mp.Manager().dict(), []
with mp.Pool(25) as p:
for _ in range(33):
rets.append(p.apply_async(run))
while rets:
for ret in rets[:]:
with suppress(mp.TimeoutError):
ret.get(timeout=0)
rets.remove(ret)
print(len(D))
错误:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 801, 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/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "/Users/???", line 9, in run
D[mp.current_process().name] = 'some val'
File "<string>", line 2, in __setitem__
File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 805, in _callmethod
self._connect()
File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 792, in _connect
conn = self._Client(self._token.address, authkey=self._authkey)
File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 507, in Client
c = SocketClient(address)
File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 635, in SocketClient
s.connect(address)
ConnectionRefusedError: [Errno 61] Connection refused
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/???", line 22, in <module>
ret.get(timeout=0)
File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/pool.py", line 771, in get
raise self._value
ConnectionRefusedError: [Errno 61] Connection refused
解决方案
推荐阅读
- sql - ORA-01722!为什么我不能在不出现此错误的情况下将 NUMBER 插入 NUMBER 数据字段?
- symfony - 解压 Symfony “标记的服务”
- java - Spring Boot 项目抛出“至少 1 个 bean 异常”
- tensorflow - 张量流中的豪斯多夫距离损失
- vba - 将 Outlook 项目限制为今天的日期 - VBA
- angular - Angular 6延迟加载路由问题
- unit-testing - 我应该测试继承和实现吗?
- java - 在android改造中使用其他类的回调方法获取值
- javascript - 以特定窗口大小打开链接
- sql - 查找特定事件之前的最新事件