.is_alive()

BrandonDusch's avatar
Published Jun 3, 2022
Contribute to Docs

The .is_alive() method returns True if the thread is still running and False, otherwise.

Syntax

thread_object.is_alive()

This method will return True from when the .run() method starts until just after it finishes.

Example

In the example below, two print(f'...') statements are written to show calls to thread.is_alive():

import threading
def countdown(count):
print(f'Thread alive? {thread.is_alive()}')
print("Counting down...")
while count > 0:
print(f'{count} left')
count -= 1
print("We made it!")
thread = threading.Thread(target=countdown, args=(5,))
thread.start()
thread.join()
print(f'Thread still alive? {thread.is_alive()}')
print("End of program.")

The output will look like the following:

Thread alive? True
Counting down...
5 left
4 left
3 left
2 left
1 left
We made it!
Thread still alive? False
End of program.

Codebyte Example

The following example goes further with showing where and when the .is_alive() method returns True:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy