Search code examples
pythonpyqt4qthread

How to exit from QThread if I have infinite loop inside run method


Have such run method:

def run(self):
    while True:
        new_value = self.client.read_holding_registers(self.reg_num, 1, unit=self.slave_id).registers[0]
        if new_value != self.value:
            self.value = new_value
            self.data_changed.emit(
                {"id": "{0};{1};{2};{3}".format(self.host, self.port, self.slave_id, self.reg_num),
                 "value": self.value})
            self.usleep(100)

So i need to catch the upcoming data from the connection. And I don't have condition to break this loop (only if connection is broken). So how I have to terminate from my thread?


Solution

  • Are you familiar with what a daemon is? If you want your thread to always run and exit when your whole application stops then you need to make it a daemon.