实例化子类[对象没有属性]有困难



当我尝试从子类Temperature_Controll启动或启动成员函数temp_controll时,会出现两种类型的错误。问题是while循环是在一个新线程中启动的。

我在将modbus客户端连接传递到成员函数时遇到问题。

AttributeError: 'ModbusTcpClient' object has no attribute 'modbus'

我不完全理解这个问题,因为我认为我会从主类继承modbus.client?

第二个问题是,当我注释掉rp并想从主类"访问成员函数时;database_reading";,我得到以下错误:

AttributeError: 'str' object has no attribute 'database_reading'

如何通过第二个线程执行子类方法?

class Echo(WebSocket):
def __init__(self, client, server, sock, address):
super().__init__(server, sock, address)
self.modbus = client
def database_reading(self)
do_something()
return data

class Temperature_Controll2(Echo):
def __init__(self, client):
super(Temperature_Controll, self).__init__(client)
self.modbus = client

def temp_controll(self, value):
#super().temp_controll(client)
while True:
print("temp_controll")
rp = self.modbus.read_coils(524, 0x1)
print(rp.bits[0])
self.database_reading()

def main():
logging.basicConfig()
with ModbusClient(host=HOST, port=PORT) as client:
client.connect()
time.sleep(0.01)
print("Websocket server on port %s" % PORTNUM)
server = SimpleWebSocketServer('', PORTNUM, partial(Echo, client))

control = Temperature_Controll2.temp_controll
t2 = threading.Thread(target=control, args=(client, 'get'))
t2.start()

try:
t1 = threading.Thread(target=server.serveforever())
t1.start()
finally:
server.close()
if __name__ == "__main__":
main()

这是我的代码的一个最简单的例子,线程t1的执行没有任何问题。我对OOP编程没有什么经验,也许这里有人可以帮忙,谢谢!

您会得到以下错误:

AttributeError: 'ModbusTcpClient' object has no attribute 'modbus'

因为当您创建的Thread

t2 = threading.Thread(target=control, args=(client, 'get'))

调用Temperature_Controll2.temp_controll(client, 'get')

在这一行:rp = self.modbus.read_coils(524, 0x1)self实际上是您在这里创建的client变量:

with ModbusClient(host=HOST, port=PORT) as client:

并且不是CCD_ 10的一个实例,我想您已经预料到了。

好的,再次感谢,解决方案是:

class Temperature_Controll2(Echo):
def __init__(self, client):
#super(Temperature_Controll2, self).__init__() #client , server, sock, address, database_reading)
#super().__init__()
self.modbus = client
def temp_controll(self, value):
#super().temp_controll(client)
while True:
print("temp_controll")
rp = self.modbus.read_coils(524, 0x1)
time.sleep(4)
def main():
with ModbusClient(host=HOST, port=PORT) as client:
client.connect()
time.sleep(0.01)
print("Websocket server on port %s" % PORTNUM)
server = SimpleWebSocketServer('', PORTNUM, partial(Echo, client))
control = Temperature_Controll2(client)
t2 = threading.Thread(target=control.temp_controll('get'))
try:
t1 = threading.Thread(target=server.serveforever())
t1.start()
finally:
server.close()

但对于客户端,我只能与modbus服务器进行一次连接,因此websocket或while循环都可以工作。我想我必须以不同的方式来处理这个问题。

我想,这是一个简短的附录,我现在知道为什么第二个变体不起作用了。

这个变体一直运行,直到类Echo中的temp_control到达一个函数调用modbus modul的点。Modbus模块不是母类Echo的一部分,这就是为什么我认为这是不能继承的。

Modbus作为一个变量通过分部传递给类Echo,从而实例化(我希望我表达得正确)。因此,只有变量客户端传递给实例的变体才能工作。

# This is a non-functional version of my programme and is for information only
class Echo(WebSocket):
def __init__(self, client, server, sock, address):
super().__init__(server, sock, address)
self.modbus = client
def temp_control(self)
do_something()
return True
class Temperature_Control3(Echo):
def __init__(self, value=None): #, client, server, sock, address):
#super(Temperature_Control3, self).__init__(server, sock, address) 
if value is None:
value = {}
self.value = value
def control(self, value):
while True:
self.temp_control(524, 'get')
#self.database_reading()[0][1]
time.sleep(2)

def main():
with ModbusClient(host=HOST, port=PORT) as client:
client.connect()
time.sleep(0.01)
print("Websocket server on port %s" % PORTNUM)
server = SimpleWebSocketServer('', PORTNUM, partial(Echo, client))
control = Temperature_Control3()
t3 = threading.Thread(target=lambda:control.control('get')) 
t3.start()
try:
t1 = threading.Thread(target=server.serveforever())
t1.start()
for thread in threading.enumerate():
print(thread.name)
finally:
server.close()

最新更新