Python threading isAlive














































Python threading isAlive



is_Alive()

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:






Comments