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()



相关文章:

  • 2022-12-23
  • 2021-04-06
  • 2021-09-05
  • 2022-01-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-14
  • 2021-08-08
  • 2022-12-23
相关资源
相似解决方案