python 难以示例化子类[对象没有属性]

w8f9ii69  于 2022-11-21  发布在  Python
关注(0)|答案(3)|浏览(110)

当我试图从子类Temperature_Controll中启动成员函数temp_controll时,我遇到了两种类型的错误。问题是while循环在一个新线程中启动。
我在将modbus客户端连接传递到成员函数时遇到了麻烦。

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

我不完全理解这个问题,因为我假设我会从主类继承modbus.client。
第二个问题是,当我注解掉rp并想从主类“database_阅读”访问一个成员函数时,我得到了以下错误:

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编程没有什么经验,也许这里有人可以帮助,谢谢!

1sbrub3j

1sbrub3j1#

您会收到此错误:

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:
而不是您所期望的Temperature_Controll2的示例。

kpbwa7wx

kpbwa7wx2#

好了,再次感谢,解决的办法是:

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循环都可以工作,我想我必须用不同的方法来解决这个问题。

8gsdolmq

8gsdolmq3#

一个简短的增编,我想,我现在知道为什么第二个变体不起作用了。
这个变量一直在运行,直到类Echo中的temp_control到达一个点,其中modbus模块被一个函数调用。Modbus模块不是母类Echo的一部分,这就是为什么我认为这不能被继承。
Modbus作为变量通过partial传递给类Echo,因此将示例化(我希望我的表达是正确的)。因此,只有变量client传递给示例的变体才能工作。

# 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()

相关问题