Jasar Orion
Jasar Orion

Reputation: 686

Verify in python if a thread exists before it run or in a while True

I have some process running in threads in my python script but id like to know if this process stops for any reason and try to execute it again. How to do that ?

my code:

main.py

from functions import Functions
func = Functions()
func.checkRoiProcess(roi_list) # this function call a thread
while True:
    # this is where i must to check if this tread still running
    #if func.checkRoiProcess still running:
    #    do some thing
    #else:
    #    execute thread again

in my functions.py

def checkRoiProcess(self,ROI):      
        Thread(target = self.checkRoiChanges, args = (ROI,), daemon=True).start()

any one can help me?

Upvotes: 0

Views: 103

Answers (1)

rdas
rdas

Reputation: 21275

Return the Thread object from the checkRoiProcess method.

def checkRoiProcess(self,ROI):      
        roi_thread = Thread(target = self.checkRoiChanges, args = (ROI,), daemon=True)
        roi_thread.start()
        return roi_thread

And use the is_alive method to check if the thread is still running:

roi_thread = func.checkRoiProcess(roi_list)
while True:
    if roi_thread.is_alive():
    #    do some thing
    else:
    #    execute thread again

Upvotes: 1

Related Questions