修改multiprocessing.Manager().dict()中对象的变量的值

复制代码
class SystemSta:
    def __init__(self):
        self.AllJointsFindedHome = False

创建字典

复制代码
manager = multiprocessing.Manager()
shared_data = manager.dict()
shared_data['SystemSta'] = SystemSta()

更改字典中对象的变量的值

错误示范

复制代码
shared_data['SystemSta'].AllJointsFindedHome = False

在Python的multiprocessing模块中,multiprocessing.Manager().dict()中的对象不支持直接修改其属性。你试图修改shared_data['SystemSta'].AllJointsFindedHome,但是这个改变并没有被应用到shared_data里。为了解决这个问题,你可以在修改SystemSta对象后,再将其赋值回shared_data,如下所示:

复制代码
    temp = shared_data['SystemSta']
    temp.AllJointsFindedHome = True
    shared_data['SystemSta'] = temp

这样,当你修改SystemSta对象的属性后,这个改变就会被正确的应用到shared_data里。

相关推荐
jiuri_12152 小时前
Docker使用详解:在ARM64嵌入式环境部署Python应用
python·docker·容器
chenchihwen2 小时前
AI代码开发宝库系列:Function Call
人工智能·python·1024程序员节·dashscope
汤姆yu4 小时前
基于python的化妆品销售分析系统
开发语言·python·化妆品销售分析
上去我就QWER4 小时前
Python下常用开源库
python·1024程序员节
程序员杰哥6 小时前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
Jyywww1216 小时前
Python基于实战练习的知识点回顾
开发语言·python
朝朝辞暮i7 小时前
从0开始学python(day2)
python
程序员黄同学7 小时前
Python中的列表推导式、字典推导式和集合推导式的性能和应用场景?
开发语言·python
AI小云7 小时前
【Python高级编程】类和实例化
开发语言·人工智能·python