我的代码由队列线程类(称为MyThread
),继承MyThread
(设备)的类和许多继承Device
的类(例如A
)组成, B
,...)
Device
实例化FSM(状态机)。因此,每个设备(A
,B
,..)都具有相同的FSM表。
现在我正在尝试为每个设备A
,B
实现FSM的回调,...
这是我的代码草图:
1.Device module:
MAP = {
'initial' : 'none',
'events': [
{'name': 'load', 'src': ['OFF', 'ON', 'none'], 'dst': 'LOADED'},
],
'callbacks': {}
}
class Device(MyThread):
def __init__(self, actor=None):
MyThread.__init__(self, actor, self.__class__)
self.MAP =MAP
self.MAP['callbacks']['onload'] = lambda e: self.initialise()
self.start()
def startFSM(self):
self.started = True
self.fsm = Fysom(self.MAP)
self.fsm.startup()
2.MyThread模块:
class MyThread(threading.Thread):
def __init__(self, actor, name):
threading.Thread.__init__(self, name=name)
self.actor = actor
def run(self):
pass
3.A:
class A(Device):
def __init__(self, actor=None):
Device.__init__(self, actor)
self.status = None
def initialise(self):
print "Here is A"
4.B:
class B(Device):
def __init__(self, actor=None):
Device.__init__(self, actor)
self.status = None
def initialise(self):
print "Here is B"
简化当前代码:
a = A()
b = B()
a.fsm.load()
b.fsm.load()
将返回:
Here is B
Here is B
而不是:
Here is A
Here is B
或再次:
a.MAP['callbacks']['onload']
与b.MAP['callbacks']['onload']
这里的重点是关注MAP属性。问题是self.MAP['callbacks']['onload']
中保存的函数会覆盖最后一个。但基本上他们每个设备应该有一个MAP(A,B,......)。每个设备都应该将自己的initialise
方法保存到自己的MAP属性中。
答案 0 :(得分:4)
问题在于:
MAP = {
'initial' : 'none',
'events': [
{'name': 'load', 'src': ['OFF', 'ON', 'none'], 'dst': 'LOADED'},
],
'callbacks': {}
}
class Device(MyThread):
def __init__(self, actor=None):
MyThread.__init__(self, actor, self.__class__)
self.MAP =MAP # Problem!
您正在向self.MAP
MAP
引用。因此,继承自Device
的类的每个实例都以self.MAP
属性结束,该属性是对完全相同的dict(全局MAP
dict)的引用。你真正想要的是这个,所以每个实例都有自己的dict:
from copy import deepcopy
MAP = {
'initial' : 'none',
'events': [
{'name': 'load', 'src': ['OFF', 'ON', 'none'], 'dst': 'LOADED'},
],
'callbacks': {}
}
class Device(MyThread):
def __init__(self, actor=None):
MyThread.__init__(self, actor, self.__class__)
self.MAP = deepcopy(MAP) # Make sure each MAP is unique.