定时器timer类

timer类

  Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法。

构造方法: 
Timer(interval, function, args=[], kwargs={}) 
  interval: 指定的时间 
  function: 要执行的方法 
  args/kwargs: 方法的参数

实例方法: 
Timer从Thread派生,没有增加实例方法。

from threading import Timer
import threading
#class threading.Timer(interval, function, args=[], kwargs={})
# def fun(dd):
# print(dd)




#1定时器用法
# timer = threading.Timer(5, fun)
# timer.start()


#2定时器用法
# if __name__=='__main__':
# t = Timer(5.0, fun)
# t.start()
# 5秒后, "hello, world"将被打印


#带参数玩法
# def fun(dd):
# print(dd)
# return dd
# timer = threading.Timer(5, fun,args=('hello',)) #args必须是带',' ,可迭代对象
#
# timer.start()


#判断玩法
def inp(dd):
print(dd)
return dd



timer = threading.Timer(5, inp,args=('1',))
if inp(1)==1:
# timer.cancel() #取消定时
timer.start()
timer.cancel()



原文地址:https://www.cnblogs.com/sunny666/p/10008721.html