Home »
Python »
Python Reference »
Python Thread class
Python Thread is_alive() Method with Example
Python Thread.is_alive() Method: In this tutorial, we will learn about the is_alive() method of Thread class in Python with its usage, syntax, and examples.
By Hritika Rajput Last updated : April 24, 2023
Python Thread.is_alive() Method
The Thread.is_alive() method is an inbuilt method of the Thread class of the threading module, it is used to check whether that thread is alive or not, ie, it is still running or not. This method returns True before the run() starts until just after the run() method is executed.
Module
The following module is required to use is_alive() method:
import threading
Class
The following class is required to use is_alive() method:
from threading import Thread
Syntax
The following is the syntax of is_alive() method:
is_alive()
Parameter(s)
The following are the parameter(s):
Return Value
The return type of this method is <class 'bool'>, it returns True is the thread is alive else returns False.
Example of Thread.is_alive() Method in Python
# Python program to explain the
# use of is_alive() method
import time
import threading
def thread_1(i):
time.sleep(5)
print('Value by Thread 1:', i)
def thread_2(i):
print('Value by Thread 2:', i)
# Creating three sample threads
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
# Before calling the start(), both threads are not alive
print("Is thread1 alive:", thread1.is_alive())
print("Is thread2 alive:", thread2.is_alive())
print()
thread1.start()
thread2.start()
# Since thread11 is on sleep for 5 seconds, it is alive
# while thread 2 is executed instantly
print("Is thread1 alive:", thread1.is_alive())
print("Is thread2 alive:", thread2.is_alive())
Output
Is thread1 alive: False
Is thread2 alive: False
Value by Thread 2: 2
Is thread1 alive: True
Is thread2 alive: False
Value by Thread 1: 1