1

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?

Artem Rys
  • 149
  • 9

1 Answers1

0

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.

limbo
  • 684
  • 9
  • 18
  • Thanks for the answer. I want to control starting and stopping my thread. If I want to create aroung 100 of this threads to run infinite, what technology have I use? – Artem Rys Jun 20 '16 at 22:01
  • when do you want to stop them and how? Do you want to manually select and stop 1 at a time? I suggested the daemon approach since you can spawn 100 of them in your main function and then when you close your application it will automatically close all the daemon threads. – limbo Jun 20 '16 at 22:18
  • I want to stop them from the interface button (start/stop). Yes, I want to select and stop 1 at a time. But I think there is a problem with a daemon, that I can't emit signal to transfer data to the interface. Or can?) As I know, you can only make signal in a class that inherits QObject. So I need to create class that inhertits run.Daemon (from [link](https://pypi.python.org/pypi/daemons)) and QObject? – Artem Rys Jun 20 '16 at 22:24
  • In your case you don't need daemon threads. [Here](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python) is something I found useful. You basically make a custom thread that before it runs it checks for a condition. Instead of `while True` you do something like `while self.isStillGoodToRun`. When you want the thread to stop you just call the `stop()` method on it. Your stop method should just set that thread specific variable (`self.isStillGoodToRun`) to `False`. – limbo Jun 20 '16 at 22:45
  • Also make sure that variable is local to the thread and not shared since it could terminate all 100 simultaneously. – limbo Jun 20 '16 at 22:50
  • Thank you for the answer. I think that i helped. Very interesting and simple solution. – Artem Rys Jun 20 '16 at 23:00
  • Do you mean the locality of the variable `self.isStillGoodToRun`? – Artem Rys Jun 20 '16 at 23:04
  • I found [something](http://www.bogotobogo.com/python/Multithread/python_multithreading_Thread_Local_Specific_Data.php) that might explain what I meant. Sorry for not providing in depth explanations and just giving links. I am not home right now. Since you will make 100 threads you need to make sure they don't use the same isStillGoodToRun. I am not really sure when it is hidden from other threads and when it is not but local() seems to do the job. I just want to make sure threadA.isStillGoodToRun can be False while threadB.isStillGoodToRun can be True at the same time. – limbo Jun 20 '16 at 23:19
  • Thank you for the explanation. I am pretty sure that my threads are running pretty separate and I will not have such issue. – Artem Rys Jun 20 '16 at 23:32