apscheduler笔记

apscheduler是一个定时框架,相当于linux中的crontab,这个框架是用python写的,用起来很方便
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from datetime import date


#任务1
def job1():
    # 输出时间
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

#任务2
def job2(text):
    print(text)
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))


# back_scheduler = BackgroundScheduler() # 运行多个任务,可以在后台执行

#当然还有其他类的介绍,大家稍微了解下就行
# BlockingScheduler: use when the scheduler is the only thing running in your process
# BackgroundScheduler: use when you’re not using any of the frameworks below, and want the scheduler to run in the background inside your application
# AsyncIOScheduler: use if your application uses the asyncio module
# GeventScheduler: use if your application uses gevent
# TornadoScheduler: use if you’re building a Tornado application
# TwistedScheduler: use if you’re building a Twisted application
# QtScheduler: use if you’re building a Qt application


# BlockingScheduler
scheduler = BlockingScheduler()  # 调度器实例化,适用于运行为唯一的任务

# 参数一:
#   运行函数
# 参数二: tigger = None 设置触发器
#   date: 特定的时间点触发 ,特点是只触发一次
# scheduler.add_job(job2, 'date',run_date=date(2019, 8, 30), args=['text1']) #Run time of job "job2 (trigger: date[2019-08-30 00:00:00 CST], next run at: 2019-08-30 00:00:00 CST)" was missed by 229 days, 16:28:57.836954
# scheduler.add_job(job2, 'date', run_date=date(2020, 4, 15,), args=['text1'])
# scheduler.add_job(job2, 'date', run_date=datetime(2020, 4, 15, 16, 33, 0), args=['text1'])
# scheduler.add_job(job2, 'date', run_date='2019-8-30 01:00:00', args=['text3'])
# scheduler.add_job(job2, 'date', run_date='2019-8-30 01:00:00', args=['text3'])
#   interval: 固定时间间隔触发
# weeks days hours minutes seconds start_date end_date timezone
scheduler.add_job(job2, 'interval', seconds=1, args=['job1']) #一秒钟触发一次
#scheduler.add_job(job2, 'interval', seconds=1, start_date='2020-04-15 16:50:00', end_date='2020-04-15 16:55:00',args=['job1'])  # 在2020-04-15 16:50:00到2020-04-15 16:55:00之间运行,期间一秒触发一次
#   cron: 在特定时间周期性地触发
#scheduler.add_job(job1, 'cron', day_of_week='1-5', hour=6, minute=30) #
scheduler.start() #运行调度程度



参考 https://www.cnblogs.com/BlueSkyyj/p/11678665.html

posted @ 2020-04-16 09:39  RainBol  阅读(111)  评论(0编辑  收藏  举报
Live2D