如何检查Threading.Timer对象当前是否在python中运行

Den*_*luğ 5 python timer

假设我定义了一个计时器,例如:

def printer(data):
    print data
data= "hello"
timer_obj = Timer(5,printer,args=[data])
timer_obj.start()
# some code
if( #someway to check timer object is currently ticking):
    #do something
Run Code Online (Sandbox Code Playgroud)

因此,有一种方法可以使计时器对象现在处于活动状态,并且活动表示我不在功能阶段而是在等待阶段。

提前致谢。

DXM*_*DXM 5

threading.Timer是threading.Thread的子类,可以使用is_alive()检查计时器是否正在运行。

import threading
import time

def hello():
    print 'hello'

t = threading.Timer(4, hello)
t.start()
t.is_alive() #return true
time.sleep(5) #sleep for 5 sec
t.is_alive() #return false
Run Code Online (Sandbox Code Playgroud)