详解Python3定时器任务代码
作者:撒欢 时间:2023-10-15 14:50:26
使用threading写的一个定时器任务demo:
import time
import sys
import signal
import datetime
import threading
#定时器
def schedule_update():
t = threading.Timer(0, event_func)
t.setDaemon(True)
t.start()
#执行函数
def event_func():
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(now_time)
exec_update()
#update_openvas_dbs_from_cache()
interval_time = delay_time()
t = threading.Timer(interval_time, event_func)
t.setDaemon(True)
t.start()
#取时间点
def delay_time():
# now time
now_time = datetime.datetime.now()
# tomorrow time
next_time = now_time + datetime.timedelta(days=+1)
next_year = next_time.date().year
next_month = next_time.date().month
next_day = next_time.date().day
# get tomorrow 00:00
next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 00:00:00", "%Y-%m-%d %H:%M:%S")
# get secondes
delay_time = (next_time - now_time).total_seconds()
return delay_time
def quit_sys(signum, frame):
sys.exit()
#接收C
if __name__ == "__main__":
try:
signal.signal(signal.SIGINT, quit_sys)
signal.signal(signal.SIGTERM, quit_sys)
schedule_update()
print("schedule_update server starting up...\nHit Ctrl-C to quit.\n")
while 1:
time.sleep(1)
except Exception as e:
print(e)
总结
以上所述是小编给大家介绍的Python3定时器任务代码,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/frisk/p/11568264.html
标签:python,定时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3实现汉语转换为汉语拼音
2022-02-28 11:27:54
![](https://img.aspxhome.com/file/2023/0/118500_0s.jpg)
pycharm自动生成文件注释和函数注释
2023-10-07 04:17:18
![](https://img.aspxhome.com/file/2023/3/69603_0s.jpg)
Python线程下使用锁的技巧分享
2023-10-03 06:05:42
详解使用navicat连接远程linux mysql数据库出现10061未知故障
2024-01-24 22:12:44
![](https://img.aspxhome.com/file/2023/5/66595_0s.png)
Python中的bytes类型用法及实例分享
2022-12-19 01:50:42
如何用VScode配置Python开发环境
2021-10-23 11:00:51
![](https://img.aspxhome.com/file/2023/5/115815_0s.gif)
Python面向对象程序设计类的多态用法详解
2021-05-28 20:21:29
Linux安装卸载Mysql数据库
2011-01-29 16:45:00
![](https://img.aspxhome.com/file/UploadPic/20111/29/2011129165230857s.jpg)
python列表详情
2023-12-29 01:59:06
前端使用svg图片改色实现示例
2022-03-14 03:54:14
![](https://img.aspxhome.com/file/2023/7/131807_0s.jpg)
tensorflow实现训练变量checkpoint的保存与读取
2023-12-15 18:10:33
![](https://img.aspxhome.com/file/2023/3/77003_0s.jpg)
Python数据预处理之数据规范化(归一化)示例
2021-11-18 12:08:10
![](https://img.aspxhome.com/file/2023/4/128574_0s.png)
10分钟搞定让你困惑的 Jenkins 环境变量过程详解
2023-09-23 13:56:03
![](https://img.aspxhome.com/file/2023/9/131839_0s.png)
vue 二维码长按保存和复制内容操作
2024-04-27 16:04:39
![](https://img.aspxhome.com/file/2023/5/133015_0s.jpg)
MySQL数据库误删回滚的解决
2024-01-19 16:31:11
![](https://img.aspxhome.com/file/2023/6/121236_0s.png)
Python socket编程实例详解
2022-02-21 09:26:03
keras 解决加载lstm+crf模型出错的问题
2022-06-11 01:27:27
Vue Element前端应用开发之常规Element界面组件
2024-04-26 17:39:32
![](https://img.aspxhome.com/file/2023/0/133160_0s.png)
如何使用Python在2秒内评估国际象棋位置详解
2023-08-10 14:26:46
![](https://img.aspxhome.com/file/2023/4/61754_0s.jpg)
解决win7操作系统Python3.7.1安装后启动提示缺少.dll文件问题
2021-08-09 00:56:29
![](https://img.aspxhome.com/file/2023/3/112743_0s.png)