Domanda

I am trying to use is_alive() to test one thread from another in Python 3.2. If i use thread1().is_alive() in thread2 i get false instead of true whether thread1 is running or not.

I get the same result if i put thread1().is_alive() inside thread1 but if i put self.is_alive() inside thread1 it returns true as expected.

It looked to me as if the command should be thread1.is_alive() but that says 1 argument required & fails.

I feel i am missing something simple but have been unable to find what that is.

A pointer would be appreciated, thanks

Ok thanks, test code looks like this:

import threading
import time
#
class FirstThread(threading.Thread):
    def run (self):
        while 1:
            print ('This is the First thread')
            print(self.is_alive())
            time.sleep(1)
FirstThread().start()
#
class TestThread (threading.Thread):
    def run ( self ):
        while 1:
            print ('This is the thread alive test thread')
            print(FirstThread().is_alive()) 
            time.sleep(1)
TestThread().start()
È stato utile?

Soluzione

Beginner mistake, the started "first" thread object is not referenced, and FirstThread().is_alive() tests an ephemeral object that is never started.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top