Python: is thread still running
You must start the thread by using threading
.
id1 = threading.Thread(target = my_function)
id1.start()
And as of above if you don't have any args
to mention you can just leave it blank.
To check your thread is alive or not you can use is_alive()
if id1.is_alive():
print("Is Alive")
else:
print("Dead")
Note: isAlive()
is deprecated instead use is_alive()
as per the python docs.
Python Documentation
The key is to start the thread using threading, not thread:
t1 = threading.Thread(target=my_function, args=())
t1.start()
Then use
z = t1.is_alive()
# Changed from t1.isAlive() based on comment. I guess it would depend on your version.
or
l = threading.enumerate()
You can also use join():
t1 = threading.Thread(target=my_function, args=())
t1.start()
t1.join()
# Will only get to here once t1 has returned.