Search code examples
pythonclassinheritancepython-multithreadingmember-functions

Difficulty instantiating a subclass [object has no attribute]


I get two types of errors when I try to start or initiate the member function temp_controll from the subclass Temperature_Controll. The issue is that the while loops are started in a new thread.

I am having trouble passing the modbus client connection to the member function.

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

I don't understand the problem in its entirety, because I assumed I would inherit modbus.client from the main class?

The second problem was, when I comment out rp and want to access a member function from the main class "database_reading", I get the following error:

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

How can I execute the subclass method via a second thread?

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

This is a minimal example of my code, the thread t1 is executed without any problems. I have little experience with OOP programming, maybe someone here can help, thanks!


Solution

  • You get this error:

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

    because when the Thread that you create:

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

    calls Temperature_Controll2.temp_controll(client, 'get'),

    on this line: rp = self.modbus.read_coils(524, 0x1) the self is actually the client variable you created here:

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

    and is not an instance of Temperature_Controll2 that I assume you were expecting.