Python3定时器任务代码
程序员文章站
2022-07-09 20:30:02
使用threading写的一个定时器任务demo: ......
使用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)
推荐阅读
-
JavaScript 定时器 SetTimeout之定时刷新窗口和关闭窗口(代码超简单)_javascript技巧
-
js用定时器实现无缝滚动效果(代码实现)
-
js延时定时器模拟qq中划过头像会显示个人信息的功能(代码)
-
MySQL定时器开启、调用实现代码
-
SpringBoot与Quartz集成实现分布式定时任务集群的代码实例
-
SpringBoot集成ElaticJob定时器的实现代码
-
springBoot定时任务处理类的实现代码
-
SpringBoot集成ElaticJob定时器的实现代码
-
ScheduledExecutorService任务定时代码示例
-
springBoot定时任务处理类的实现代码