I need to determine when a function is running in a separate thread

threading.Thread(target = self.backgroundFunction, args=(..., )) 

will complete itself, not when I interrupt / terminate the stream.

In the documentation I found only about the completion of the stream.

But if the function worked before I closed the stream, how to “catch” its completion?

  • one
    You can either wait for the thread to complete itself (join, guaranteeing the execution of the code after the function is executed, but not necessarily immediately), or you should use your own functionality called at the end of the function. - etki
  • It is not very clear what behavior you want to achieve. Try to give a more extensive example. - Timofei Bondarev
  • @Timofey Bondarev thread = threading.Thread(target = self.backgroundFunction, args=(..., )) - I select the thread. Then, in the internalStop () method, I interrupt it with thread.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_programming
  • It is not very clear what the is_alive method shows is_alive : This method returns True just before the run() method starts; it returns True when your task is executed. - Timofei Bondarev

2 answers 2

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() завершился 
  • And if I can't, as in your example in the cycle, wait for completion. If I have multiple functions? Where can I catch .isAlive ()? I tried it like this. But this is essentially meaningless, because after internalStart I can call a lot of functions, isAlive () will change, but I don’t recognize it. Here is a specific code example. hostingkartinok.com/… - ilya_leti_programming
  • @ IlyaOlchikov Updated the answer, maybe it will suit you. - Timofei Bondarev

The lifetime of the call to the function passed as target and the thread is the same, so nothing needs to be done: your_thread.join() will return when the call ends.