line 217, in get_token_num return str(self.TYPE_TO_CONTENT_DICT_OP[token]) KeyError: 'i'
时间: 2023-10-03 12:10:09 浏览: 40
这个错误通常发生在字典中找不到给定的键时。根据错误信息,似乎是在`get_token_num`函数中,使用了一个名为`token`的变量作为字典的键,但字典中没有这个键,导致了`KeyError`异常的发生。
具体来说,根据错误信息中的`'i'`,可以猜测在函数中使用了一个值为`'i'`的变量作为键,但是在`TYPE_TO_CONTENT_DICT_OP`字典中没有这个键。
解决这个问题的方法是检查一下字典中是否真的包含该键,或者在使用字典时提前设定好默认值,以避免出现找不到键的情况。例如,可以使用字典的`get`方法,如果找不到对应键,则返回一个默认值。示例如下:
```python
def get_token_num(token):
return str(TYPE_TO_CONTENT_DICT_OP.get(token, 'default_value'))
```
这样,当字典中找不到对应的键时,将返回`'default_value'`。
相关问题
FAILED test_api.py::TestApi::test_get_token - TypeError: unhashable type: 'dict'
This error occurs because you are attempting to use a dictionary as a key in a hash table, which is not possible because dictionaries are mutable and their values can change. Hash tables require keys to be immutable so that they can be hashed and looked up quickly.
To fix this error, you should convert the dictionary to an immutable data type such as a tuple, frozenset, or named tuple before using it as a key in a hash table. Alternatively, you could use a different data structure that does not require hashable keys, such as a list or a queue.
Here is an example of how you could convert a dictionary to a tuple:
```
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_tuple = tuple(sorted(my_dict.items()))
```
This converts the dictionary to a tuple of its (key, value) pairs, sorted by key, which can be used as a hashable key in a hash table.
Alternatively, you could use a frozenset:
```
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_frozenset = frozenset(my_dict.items())
```
This creates a frozenset of the (key, value) pairs, which is also immutable and can be used as a key in a hash table.
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] 系统找不到指定的文件。
这个错误通常是由于在多进程环境中使用了不支持共享的对象或属性导致的。`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` 的方式向字典中添加属性。
希望这次能够帮助你解决问题。如果还有其他疑问,请随时提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)