1. Sometimes there are some needs in the work that need to be solved by regular tasks, such as regular model training, regular algorithm detection, regular cleaning of some data in the database, etc.
2.Python has a timing task module BlockingScheduler, which can well solve the timing task needs
from apscheduler. schedulers. blocking import BlockingScheduler
my_scheduler = BlockingScheduler ( )
#每天15 :57 分执行该定时任务
my_scheduler. add_job ( delete_message_data, 'cron' , day= '*' , hour= '15' , minute= '57' )
my_scheduler. start ( )
def delete_message_data ( ) :
print ( 11111111111 )
scheduler. add_job ( job, 'cron' , hour= 1 , minute= 5 )
hour = 19 , minute = 23 这里表示每天的19 :23 分执行任务
hour = '19' , minute = '23' 这里可以填写数字,也可以填写字符串
hour = '19-21' , minute= '23' 表示 19 : 23 、 20 : 23 、 21 : 23 各执行一次任务
#每300 秒执行一次
scheduler . add_job ( job, 'interval' , seconds= 300 )
#在1 月, 3 月, 5 月, 7 - 9 月,每天的下午2 点,每一分钟执行一次任务
scheduler . add_job ( func= job, trigger= 'cron' , month= '1,3,5,7-9' , day= '*' , hour= '14' , minute= '*' )
# 当前任务会在 6 、7 、8 、11 、12 月的第三个周五的 0 、1 、2 、3 点执行
scheduler . add_job ( job, 'cron' , month= '6-8,11-12' , day= '3rd fri' , hour= '0-3' )
#从开始时间到结束时间,每隔俩小时运行一次
scheduler . add_job ( job, 'interval' , hours= 2 , start_date= '2018-01-10 09:30:00' , end_date= '2018-06-15 11:00:00' )
#自制定时器
from datetime import datetime
import time
# 每n秒执行一次
def timer ( n) :
while True:
print ( datetime. now ( ) . strftime ( "%Y-%m-%d %H:%M:%S" ) )
time. sleep ( n)
timer ( 5 )