我正在尝试使用 is_alive() 在 Python 3.2 中测试另一个线程。如果我使用thread1().is_alive()inthread2我得到false而不是true是否 thread1正在运行。
如果我放在里面,我会得到相同的结果,但如果我放在
thread1().is_alive()里面,它会按预期返回。thread1self.is_alive()thread1true
在我看来,命令应该是thread1.is_alive(),但它说需要 1 个参数并且失败。
我觉得我错过了一些简单的东西,但一直找不到那是什么。
指点将不胜感激,谢谢
好的,谢谢,测试代码如下所示:
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()