修改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里。

相关推荐
databook1 分钟前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风1 小时前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风1 小时前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei17 小时前
python 抽象基类
python
用户83562907805117 小时前
Python 实现 PPT 转 HTML
后端·python
zone77391 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77391 天前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒2 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习2 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽2 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama