python运行results = pool.map(self.face_recognize, numbers)报错TypeError: Pickling an AuthenticationString object is disallowed for security reasons
时间: 2024-03-27 10:40:38 浏览: 22
这个错误通常出现在使用 Python 中的 `multiprocessing.Pool` 对象执行并行任务时,其中的任务涉及到了无法序列化的对象,例如 `AuthenticationString`。
在执行任务之前,`multiprocessing.Pool` 会将任务序列化,然后将其发送给子进程进行处理。但是,由于某些对象无法被序列化,因此就会出现这个错误。
为了解决这个问题,你可以考虑在执行任务之前,将涉及到的 `AuthenticationString` 对象转换为其他类型的对象,例如字符串或字节串。这样,`multiprocessing.Pool` 在序列化任务时就可以避免序列化这些敏感对象,从而避免出现安全问题。
具体来说,你可以将 `face_recognize` 函数中使用到的 `AuthenticationString` 对象转换为字符串或字节串,例如:
```python
def face_recognize(number):
# 将 AuthenticationString 对象转换为字符串或字节串
password = str(authentication_string)
...
```
或者,你也可以使用其他的并行计算库,例如 `concurrent.futures` 或 `joblib`,它们可以更好地处理一些无法被序列化的对象。
总之,为了保证代码的安全性和可靠性,你应该尽可能避免在并行计算中使用无法被序列化的对象,或者使用更加安全和可靠的并行计算库来代替 `multiprocessing.Pool`。
相关问题
运行results = pool.map(self.face_recognize, numbers)报错Pickling an AuthenticationString object is ' TypeError: Pickling an AuthenticationString object is disallowed for security reasons
这个错误通常是由于 `multiprocessing.Pool` 在序列化参数时遇到了不能被序列化的对象,导致无法使用 `map` 方法。在你的情况下,错误是由于 `multiprocessing` 无法序列化 `AuthenticationString` 对象引起的。
要解决这个问题,你需要确保所有参数都可以被序列化。如果你的 `face_recognize` 方法需要使用 `AuthenticationString` 对象,那么你需要找到一个方法来避免在序列化时使用它。
一种解决方法是在 `face_recognize` 方法中使用字符串替代 `AuthenticationString` 对象。另一种方法是在调用 `map` 方法之前,将 `AuthenticationString` 对象转换为一个可以序列化的对象,例如字符串或字节数组。
例如,你可以尝试将 `AuthenticationString` 对象转换为字符串,然后在 `face_recognize` 方法中使用该字符串。示例代码如下:
```python
import base64
def face_recognize(number):
auth_str = base64.b64encode(authentication_string().encode('utf-8')).decode('utf-8')
# 在这里使用 auth_str 来进行人脸识别
# ...
return result
auth_str = authentication_string()
# 将 AuthenticationString 对象转换为字符串
auth_str_str = base64.b64encode(auth_str.encode('utf-8')).decode('utf-8')
# 将转换后的字符串作为参数传递给 map 方法
results = pool.map(face_recognize, [(number, auth_str_str) for number in numbers])
# 在 face_recognize 方法中重新转换为 AuthenticationString 对象
def face_recognize_wrapper(args):
number, auth_str_str = args
auth_str = authentication_string(auth_str_str)
return face_recognize(number, auth_str)
results = pool.map(face_recognize_wrapper, [(number, auth_str_str) for number in numbers])
```
这样做可以确保所有参数都可以被序列化,从而避免出现 `TypeError: Pickling an AuthenticationString object is disallowed for security reasons` 的错误。
运行results = pool.map(face_recognize, numbers)报错AttributeError: Can't pickle local object 'RockxFaceRecognition.run.<locals>.face_recognize'
这个错误通常是由于 multiprocessing 库无法序列化某些对象引起的。在您的情况下,似乎是由于 `face_recognize` 函数中的某些本地对象无法序列化。
为了解决这个问题,您可以尝试将 `face_recognize` 函数定义为一个独立的函数,而不是在 `run` 方法内部定义的本地函数。或者,您可以使用 `dill` 库对 `face_recognize` 函数进行序列化和反序列化,以便 multiprocessing 库可以正确地处理它。
具体来说,您可以尝试使用以下代码:
```
import dill
def run(self):
# ...
with multiprocessing.Pool(processes=4) as pool:
results = pool.map(dill.loads(dill.dumps(face_recognize)), numbers)
# ...
```
使用 `dill.loads(dill.dumps(face_recognize))` 将 `face_recognize` 函数序列化并反序列化,从而允许 multiprocessing 库正确地处理它。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)