For your purpose, the is_alive object's is_alive method is is_alive .
This method returns the method terminates.
My free translation: the method returns True from the start of the run() method until the end of its execution.
Consider an example:
import time import threading def sleep_5_seconds(): time.sleep(5) t = threading.Thread(target=sleep_5_seconds) print(t.is_alive()) # False t.start() print(t.is_alive()) # True for i in range(6): time.sleep(1) print(t.is_alive()) # True все, кроме последних print(t.is_alive()) # False t.join() # выполнится мгновенно, так как поток отработал
As soon as the method starts issuing False , the function launched in it is executed.
Update:
If you want to execute some code after the function is executed, you can simply combine these two functions into one and pass it to the thread for execution:
import time import threading def sleep_5_seconds(): time.sleep(5) def after_finish(): print('Task was finished') def aggregate(): sleep_5_seconds() after_finish() t = threading.Thread(target=aggregate) t.start() # через 5 секунд вы увидите сообщение в консоли => run() завершился
thread = threading.Thread(target = self.backgroundFunction, args=(..., ))- I select the thread. Then, in the internalStop () method, I interrupt it withthread.join(). But the fact is that the function can exit before I call the internalStop () method. How can I catch it? I tried thread.isAlive (), but it does not show what I need. - ilya_leti_programmingis_alivemethod showsis_alive: This method returnsTruejust before therun()method starts; it returnsTruewhen your task is executed. - Timofei Bondarev