gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 30 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章利用Python实现定时程序的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

定时器概念

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

实现一个简单的定时程序

方案一

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

?
1
2
3
4
5
def main():
     startCronTask(userCountFunc, minutes = 60 )
 
if __name__ = = '__main__' :
     main()

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

?
1
2
3
4
5
6
7
from apscheduler.schedulers.blocking import BlockingScheduler
 
def startCronTask(task, * * config):
     # BlockingScheduler
     scheduler = BlockingScheduler()
     scheduler.add_job(task, 'interval' , * * config)
     scheduler.start()

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

?
1
2
3
def userCountFunc():
     logger.info( 'count user' )
     ...

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

方案二

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

?
1
2
3
4
5
6
7
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,我们发现上面在执行后并未循环执行,所以需要修改下

?
1
2
3
4
5
6
7
8
9
10
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停止定时器的工作,如下例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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 。

到此这篇关于利用Python实现定时程序的方法的文章就介绍到这了,更多相关Python 定时程序内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://juejin.cn/post/6981328254523146248 。

最后此篇关于利用Python实现定时程序的方法的文章就讲到这里了,如果你想了解更多关于利用Python实现定时程序的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com