网络编程
位置:首页>> 网络编程>> Python编程>> 利用Python实现定时程序的方法

利用Python实现定时程序的方法

作者:Lavender1  发布时间:2021-04-16 08:28:04 

标签:Python,定时程序
目录
  • 定时器概念

  • 实现一个简单的定时程序

    • 方案一

    • 方案二

定时器概念

什么是定时器呢?它是指从指定的时刻开始,经过一个指定时间,然后触发一个事件,用户可以自定义定时器的周期与频率。

实现一个简单的定时程序

方案一

在 Python 中,如何定义一个定时器函数呢?我们先看第一种方法。假设我们需要执行一个函数userCountFunc,这个函数需要每隔一个小时被执行一次。那么,我们可以这样写:


def main():
   startCronTask(userCountFunc, minutes=60)

if __name__ == '__main__':
   main()

如上面的代码,我们在定义了一个 main 函数后,便定义了一个定时函数 startCronTask。第一个参数为函数名,第二个参数为时间,第二个参数表示多长时间后调用后面第一个参数的函数。第一个参数注意是函数对象,进行参数传递,用函数名(如 userCountFunc)表示该对象,不是函数执行语句 userCountFunc(),不然会报错。那么,在实现这个函数时,需要引入定时功能,Python 中有一个定时任务模块 BlockingScheduler:


from apscheduler.schedulers.blocking import BlockingScheduler

def startCronTask(task, **config):
   # BlockingScheduler
   scheduler = BlockingScheduler()
   scheduler.add_job(task, 'interval', **config)
   scheduler.start()

定义完一个调度模块之后,实际的定时调度功能就完成了。接下来,需要具体实现定时执行的逻辑函数 userCountFunc:


def userCountFunc():
   logger.info('count user')
   ...

这样,对于方案一,实现的简单的定时功能就完成了。

方案二

方案一中介绍的是 Python 自带的 BlockingScheduler 模块,Python 中除了可以通过 BlockingScheduler,还通过线程实现定时器 timer,来简单的看下代码:


import threading

def timerFunc():
   print('Hello World~')

timer = threading.Timer(1, timerFunc)
timer.start()

在上面的代码中,定时器函数 threading.Timer 主要有2个参数,参数意义与方案一类似,接下来执行这段程序:

Hello World~
      
Process finished with exit code 0

我们发现只执行一遍,程序就结束了,但显然不是我们想要的结果。其实,我们看下 Time 类,有这样的一句解释性注释:Call a function after a specified number of seconds,我们发现上面在执行后并未循环执行,所以需要修改下:


import threading

def timerFunc():
   print('Hello World~')
   global timer
   timer = threading.Timer(10.5, timerFunc)
   timer.start()

timer = threading.Timer(3, timerFunc)
timer.start()

此时,我们可以看到输出结果:

Hello World~

Hello World~

Hello World~
...

这里需要注意的是:必须在定时器执行函数内部重复构造定时器,因为定时器构造后只执行1次,必须循环调用。

另外,在上面的代码中,我们其实还可以看到:threading.Timer(5.5, timerFunc),定时器间隔单位是秒,可以是浮点数,如5.5,0.9等,在执行函数 timerFunc 内部和外部中给的值可以不同。如上例中第一次执行 timerFunc 是3秒后,后面的都是10.5秒后执行。

接下来,我们再看看如何再一定时间结束定时功能。我们可以使用cancel停止定时器的工作,如下例:


import threading

def timerFunc():
   print('Hello World~')
   global timer
   timer = threading.Timer(10.5, timerFunc)
   timer.start()

timer = threading.Timer(3, timerFunc)
timer.start()

time.sleep(60)
timer.cancel()

上面的代码表示:在定时器按照一定时间执行后,执行过程耗时60秒后停止定时操作功能,退出。显示结果为:

Hello World~

Hello World~

Hello World~

Hello World~

Hello World~
...

Process finished with exit code 0

来源:https://juejin.cn/post/6981328254523146248

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com