.is_alive()
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 threadingdef countdown(count):print(f'Thread alive? {thread.is_alive()}')print("Counting down...")while count > 0:print(f'{count} left')count -= 1print("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? TrueCounting down...5 left4 left3 left2 left1 leftWe made it!Thread still alive? FalseEnd of program.
Codebyte Example
The following example goes further with showing where and when the .is_alive()
method returns True
:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours