is_Alive() method checks whether a thread is alive and returns a boolean value based on the thread status. is_Alive() method is often used in thread synchronisation so as to check whether a thread is alive before sending any signals to it. However efforts need to be taken to have status of the latest thread status from a most recent call to is_Alive().
True - When a thread is alive
False - When a thread has been already terminated completely
This method returns true just before the run( ) method starts until just after the run( ) method terminates. The module function enumerate( ) returns a list of all alive threads, a process object is alive from the moment the start( ) method returns until the child process terminates.
Source code:
import threading
import time
def thread1():
print('Starting the thread1')
print('Exiting the thread1')
def thread2():
print('Starting the thread2 ')
time.sleep(5)
print('Exiting the thread2')
if __name__ == '__main__':
t1 = threading.Thread(name='non-daemon', target=thread1)
thread = threading.Thread(name='daemon', target=thread2)
thread.setDaemon(True)
thread.start()
t1.start()
thread.join(2)
print ('thread.isAlive()', thread.isAlive())
t1.join()
Output:
Name | Views | Likes |
---|---|---|
Python threading Timer | 890 | 0 |
Python threading getName | 423 | 0 |
Program to count "Number of Vowels" in the string | 350 | 0 |
Python threading isAlive | 1589 | 0 |
Python threading introduction | 811 | 0 |
Python threading setName | 1216 | 0 |
Python threading isDaemon | 742 | 0 |
Python threading get_ident | 470 | 0 |
Python threading currentThread | 459 | 0 |
Python threading active_count | 441 | 0 |
Python threading enumerate | 508 | 0 |
Python threading setDaemon | 1062 | 0 |
Python threading main_thread | 432 | 0 |
Python threading cancel | 504 | 0 |
Comments