Saturday, 19 December 2015

Python: is_alive(): Check whether thread is alive

'is_alive()' method returns true, if the thread is live, else false.


MyThread.py
import threading
import time

class MyThread(threading.Thread):
 def run(self):
  print(threading.current_thread().getName()," with id ",threading.get_ident()," Started")
  time.sleep(1)
  print(threading.current_thread().getName()," with id ",threading.get_ident()," Finished")
  

thread1 = MyThread(name="Thread_1")
thread1.start()

print("Is thread1 alive ", thread1.is_alive())
time.sleep(2)
print("Is thread1 alive ", thread1.is_alive())

$ python3 MyThread.py 
Thread_1  with id  4336459776  Started
Is thread1 alive  True
Thread_1  with id  4336459776  Finished
Is thread1 alive  False



Previous                                                 Next                                                 Home

No comments:

Post a Comment