Python用thread做个简单定时器
2023-04-09 16:30

很多语言都有定时器功能用于触发一些事件。有些语言封装成一个函数调用,有些要自己写代码,今天就写一个python的定时器:

import threading
def fun_timer():
      print('Hello world')
      global timer
      timer = threading.Timer(5.5, fun_timer)
      timer.start()
timer = threading.Timer(1, fun_timer)
timer.start()

输出结果:
Hello world
Hello world
Hello world
Hello world

............
每隔5.5秒输出一次,定时器工作正常,这是一个死循环除非手动结束不然不会停止。

电脑 & 软件 | 相关: 分享:国外免费U盘制作工具:Rufus