class MonitoringProcess: def __init__(self): self.conn1, self.conn2 = Pipe() self.monitor = True self.process_start(self.detection_status) def set_monitor(self): self.com_dict.monitor = False def process_start(self, func): with Manager() as manager: self.com_dict = manager.Namespace() p = Process(target=func, args=(self.com_dict,)) p.start() def detection_status(self, com_dict): # some code ... com_dict.a = 1 Process Process-2: Traceback (most recent call last): File "C:\Python38\lib\multiprocessing\managers.py", line 827, 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 "C:\Python38\lib\multiprocessing\process.py", line 315, in _bootstrap self.run() File "C:\Python38\lib\multiprocessing\process.py", line 108, in run self._target(*self._args, **self._kwargs) File "F:\E\python_learn\我的框架\自动化框架2\monitoring.py", line 24, in detection_status com_dict.a = 1 File "C:\Python38\lib\multiprocessing\managers.py", line 1143, in __setattr__ return callmethod('__setattr__', (key, value)) File "C:\Python38\lib\multiprocessing\managers.py", line 831, in _callmethod self._connect() File "C:\Python38\lib\multiprocessing\managers.py", line 818, in _connect conn = self._Client(self._token.address, authkey=self._authkey) File "C:\Python38\lib\multiprocessing\connection.py", line 500, in Client c = PipeClient(address) File "C:\Python38\lib\multiprocessing\connection.py", line 702, in PipeClient _winapi.WaitNamedPipe(address, 1000) FileNotFoundError: [WinError 2] 系统找不到指定的文件。
时间: 2024-04-14 15:27:53 浏览: 138
conn_sql.rar_CONN_SQL.asp_asp conn sql_conn sql_conn.asp
这个错误通常是由于在多进程环境中使用了不支持共享的对象或属性导致的。`AttributeError: 'ForkAwareLocal' object has no attribute 'connection'` 错误表明在创建 `com_dict` 属性时出现了问题。
在你的代码中,你正在尝试在 `detection_status` 方法中给 `com_dict` 添加属性 `a`,但在多进程环境中,不能直接在命名空间对象上动态添加属性,因为它不是共享的。
要解决这个问题,你可以使用 `manager.dict()` 来创建一个共享的字典对象,然后在 `detection_status` 方法中使用该字典对象来添加属性。以下是修正后的示例代码:
```python
from multiprocessing import Process, Manager, Pipe
class MonitoringProcess:
def __init__(self):
self.conn1, self.conn2 = Pipe()
self.monitor = True
self.process_start(self.detection_status)
def set_monitor(self):
self.com_dict["monitor"] = False
def process_start(self, func):
with Manager() as manager:
self.com_dict = manager.dict()
p = Process(target=func, args=(self.com_dict,))
p.start()
def detection_status(self, com_dict):
# some code ...
com_dict["a"] = 1
```
在修正后的代码中,我们使用 `manager.dict()` 创建了一个共享的字典对象 `com_dict`。然后,在 `detection_status` 方法中,我们使用 `com_dict["a"] = 1` 的方式向字典中添加属性。
希望这次能够帮助你解决问题。如果还有其他疑问,请随时提问。
阅读全文