How to check a thread is alive in python multithreaded programs.?

Humble Devassy Chirammal Forums Python Programming How to check a thread is alive in python multithreaded programs.?

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1932 Reply
    Humble
    Keymaster

    Last week I received this query from my colleague and the explanation was this:

    Its common practice that, making the main thread wait for the threads ‘started’ by it . Its done with .join() method of ‘Thread’ class from ‘threading’ module. How-ever, sometimes, instead of waiting ‘indefinitely’ for the thread to join , most use a timeout value in join. But it does not guarantee that, the ‘thread’ is alive or not even after hitting the timeout. To check the threads ‘aliveness’ you can use ‘t.isAlive()’ method which wil return ‘true’ if its ‘alive..

    For ex:

    >for th in threads:
          th.join (30)
          if not th.isAlive():
                 logging.info ('Thread : %s terminated' % (th.getName()))
          else:
                 logging.debug( 'Thread is still alive.' )
    
    .................
    
Viewing 1 post (of 1 total)
Reply To: How to check a thread is alive in python multithreaded programs.?
Your information: