加入收藏 | 设为首页 | 会员中心 | 我要投稿 财气旺网 - 财气网 (https://www.caiqiwang.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

Python Timer定时 器:控制函数在规定时间实施

发布时间:2022-12-07 11:18:14 所属栏目:语言 来源:
导读:  from threading import Timer

  

  def hello():

  print("hello, world")

  # 指定10秒后执行hello函数

  t = Timer(10.0, hello)

  t.start()

  上面程序使用 Ti

  from threading import Timer
 
  
 
  def hello():
 
  print("hello, world")
 
  # 指定10秒后执行hello函数
 
  t = Timer(10.0, hello)
 
  t.start()
 
  上面程序使用 Timer 控制 10s 后执行 hello 函数。 需要说明的是,Timer 只能控制函数在指定时间内执行一次,如果要使用 Timer 控制函数多次重复执行,则需要再执行下一次调度。 如果程序想取消 Timer 的调度,则可调用 Timer 对象的 cancel() 函数。例如,如下程序每 1s 输出一次当前时间:
  from threading import Timer
 
  import time
 
  
 
  # 定义总共输出几次的计数器
 
  count = 0
 
  def print_time():
 
  print("当前时间:%s" % time.ctime())
 
  global t, count
 
  count += 1
 
  # 如果count小于10,开始下一次调度
 
  if count < 10:
 
  t = Timer(1, print_time)
 
  t.start()
 
  # 指定1秒后执行print_time函数
 
  t = Timer(1, print_time)
 
  t.start()
 
  上面程序开始运行后,程序控制 1s 后执行 print_time() 函数。print_time() 函数中的代码会进行判断,如果 count 小于 10,程序再次使用 Timer 调度 1s 后执行 print_time() 函数,这样就可以控制 print_time() 函数多次重复执行。 在上面程序中,由于只有当 count 小于 10 时才会使用 Timer 调度 1s 后执行 print_time() 函数,因此该函数只会重复执行 10 次。
 

(编辑:财气旺网 - 财气网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!