QThread im not able to resume it

I have a thread like :
class Homing(QObject):
segnale_homing = pyqtSignal(bool, bool, )

def __init__(self, parent=None):
    super().__init__(parent)
    self.pause_homing = True


def run(self):
    while self.pause_homing == True:
        print('sleep')
        time.sleep(0.3)
    while self.pause_homing == False:
        print('in the thread')
        variable_1 = self.x5.read_bool('M103')
        variable_2= self.x5.read_bool('M102')
        
        self.segnale_homing.emit(variable_1, variable_2,)

in the main widget I have in the init

self.read_homing = Homing()
self.read_homing.x5 = self.x5
self.read_homing_thread = QThread()
self.read_homing.moveToThread(self.read_homing_thread)
self.read_homing_thread.started.connect(self.read_homing.run)
self.read_homing.segnale_homing.connect(self.readed_homing)
self.read_homing_thread.start()

then I have a button :

@pyqtSlot()
def on_pushButton_43_clicked(self):
self.read_homing.pause_homing = False
self.x5.write_bool(‘M8’, True)
self.x5.write_bool(‘M325’, True)

Then I have the function connected to the signal

def readed_homing(self,variable_1,variable_2)
print(variable_,variable_2)
self.read_homing.pause_homing = True
msg = QMessageBox(QMessageBox.Icon.Warning, “variable_1”+ str(variable_1)+ "Variable_2 "+str(variable_2))
msg.setStyleSheet(“QMessageBox{ background-color: qlineargradient(spread:pad, x1:0.227, y1:0.3125, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));}”
“QPushButton{background-color: rgba(0, 0, 0,0%);border: 1px solid; border-radius: 3px;border-color: black; color: black}”
“QLabel{color:white;}”)
msg.exec()

I expect that when I set again the variable self.read_homing.pause_homing = True the thread continues to run with the “print sleep” as he do at the first start, but in reality the thread never run after I set the variable pause_homing to True.

So at the beginning he cycles in the “sleep” than when I press the button he goes in the thread and give me back the variable value, but after I set again to True the self.read_homing.pause_homing = True he stop running

What is wrong??

Hi and welcome,

Once you set pause_homing to True the second time, it goes out of your last while loop, and then ends the run function, and the thread. run is executed only once, when the thread is started.

If you want you thread keeping running forever, you need a while True, but then you need another condition to terminate your thread

def run(self):
    while True:
        if self.pause_homing:
            # sleep
        else:
            # process variables
        if self.stop:
            break

It may be better to stop the thread when you are in pause, and to start it again when you are processing.