Python timing task BlockingScheduler module

         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()
#每天1557分执行该定时任务
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  这里表示每天的1923 分执行任务
hour ='19', minute ='23'  这里可以填写数字,也可以填写字符串
hour ='19-21', minute= '23'  表示 19:2320:2321: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='*')
 
# 当前任务会在 6781112 月的第三个周五的 0123 点执行
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)

Guess you like

Origin blog.csdn.net/weixin_43697214/article/details/109384467