2016-12-26 151 views
3

比方说,我定义了一个定时器,如:如何检查是否Threading.Timer对象当前在Python运行

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 

那么,有没有一种方式,如果计时器对象是活动的,现在,通过主动我的意思不是在功能阶段,而是在等待阶段。

在此先感谢。

回答

4

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 
相关问题