flask+APScheduler定时任务的使用

APScheduler简介

APScheduler基于Quartz的一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便。提供了基于日期、固定时间间隔以及crontab类型的任务,并且可以持久化任务。基于这些功能,我们可以很方便的实现一个python定时任务系统。

组成部分
APScheduler有四种组成部分:

触发器(trigger)包含调度逻辑,每一个作业有它自己的触发器,用于决定接下来哪一个作业会运行。除了他们自己初始配置意外,触发器完全是无状态的。
作业存储(jobstore)存储被调度的作业,默认的作业存储是简单地把作业保存在内存中,其他的作业存储是将作业保存在数据库中。一个作业的数据讲在保存在持久化作业存储时被序列化,并在加载时被反序列化。调度器不能分享同一个作业存储。
执行器(executor)处理作业的运行,他们通常通过在作业中提交制定的可调用对象到一个线程或者进城池来进行。当作业完成时,执行器将会通知调度器。
调度器(scheduler)是其他的组成部分。你通常在应用只有一个调度器,应用的开发者通常不会直接处理作业存储、调度器和触发器,相反,调度器提供了处理这些的合适的接口。配置作业存储和执行器可以在调度器中完成,例如添加、修改和移除作业。
调度器

APScheduler提供了多种调度器,可以根据具体需求来选择合适的调度器,常用的调度器有:

BlockingScheduler:适合于只在进程中运行单个任务的情况,通常在调度器是你唯一要运行的东西时使用。

BackgroundScheduler: 适合于要求任何在程序后台运行的情况,当希望调度器在应用后台执行时使用。

AsyncIOScheduler:适合于使用asyncio框架的情况

GeventScheduler: 适合于使用gevent框架的情况

TornadoScheduler: 适合于使用Tornado框架的应用

TwistedScheduler: 适合使用Twisted框架的应用

QtScheduler: 适合使用QT的情况

安装

普通使用安装

pip install apscheduler

结合flask使用安装

pip install Flask-APScheduler

add_job参数详解

id
id代表该job唯一标识,不可重复,之后可以用id查找job

trigger
它管理着作业的调度方式。它可以为date, interval或者cron。对于不同的trigger,对应的参数也不同。

interval 间隔时间(每隔一段时间执行)
后面可以写的参数:

weeks (int) – number of weeks to wait
days (int) – number of days to wait
hours (int) – number of hours to wait
minutes (int) – number of minutes to wait
seconds (int) – number of seconds to wait
start_date (datetime|str) – starting point for the interval
calculation
end_date (datetime|str) – latest possible date/time to trigger on
timezone (datetime.tzinfo|str) – time zone to use for the date/time
calculations

date 定时调度(只执行一次)
run_date (datetime|str) – the date/time to run the job at -(任务开始的时间)
timezone (datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already

# The job will be executed on November 6th, 2009
sched.add_job(func=work1, trigger='date', run_date=date(2009, 11, 6), args=['text'])
# The job will be executed on November 6th, 2009 at 16:30:05
sched.add_job(func=work1, trigger='date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])

cron定时调度(某一定时时刻执行)
后面参数和interval大致相同

# 表示2017年3月22日17时19分07秒执行该程序
sched.add_job(work1, 'cron', year=2017, month=3, day=22, hour=17, minute=19, second=7)
# 表示任务在6,7,8,11,12月份的第三个星期五的00:00,01:00,02:00,03:00 执行该程序
sched.add_job(work1, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')
# 表示从星期一到星期五5:30(AM)直到2014-05-30 00:00:00
sched.add_job(work1, 'cron', day_of_week='mon-fri', hour=5, minute=30, end_date='2014-05-30')
# 表示每5秒执行该程序一次,相当于interval 间隔调度中seconds = 5
sched.add_job(work1, 'cron', second='*/5')

args 参数,要可迭代对象

job的其他操作

# 移除所有,移除要放在start之前才有效
sched.remove_all_jobs()
# 根据id移除job
sched.remove_job('my_work1')
sched.pause_job('my_work1')  # 暂停
sched.resume_job('my_work1')  # 恢复
sched.get_job('my_work1')  # 获取
sched.get_jobs()  # 获取所有job列表
# 默认情况是调度器等所有job完成后关闭,设为False时直接关闭
sched.shutdown(wait=False)  

结合flask使用

用uwsgi启动项目

(venv) [root@hmy assets-service]# cd /app/flask_project/api/app/common/apschedulerjob.py
#!/usr/bin/env python
# -*- coding: utf8 -*-

from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
import config, time, requests


def task():
    return

def update_timer(x):
    print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)




scheduler = BlockingScheduler()



# scheduler.add_job(func=update_timer, args=('定时任务',), trigger='cron', hour='*/12')
# scheduler.add_job(func=update_timer, 'cron',hour=0, minute=0,second=1)
# scheduler.add_job(func=update_timer, args=('定时任务',), trigger='cron', second='*/5')
# scheduler.add_job(func=update_timer, args=('一次性任务',), next_run_time=datetime.now() + timedelta(seconds=12))
# scheduler.add_job(func=update_timer, args=('循环任务',), trigger='interval', seconds=5)
# scheduler.add_job(func=update_timer, args=('循环任务',), trigger='interval', minutes=1)
scheduler.start()

## 下面这句加在定时任务模块的末尾...判断是否运行在uwsgi模式下, 然后阻塞mule主线程(猜测).
try:
    import uwsgi
    while True:
        sig = uwsgi.signal_wait()
except Exception as err:
    pass

用gunicorn+gevent启动flask项目

(venv) [root@hmy assets-service]# cd /app/flask_project/api/app/common/apschedulerjob.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
from apscheduler.schedulers.background import BackgroundScheduler
from pyfsops.tools import Tools
import nacos, config
from app import logger

tools = Tools()


def nacos_healthy():
    SERVER_ADDRESSES = ','.join(config.NACOS_INFO['address'])
    user = config.NACOS_INFO['user']
    password = config.NACOS_INFO['password']
    NAMESPACE = "public"  # 名称空间-命名空间
    client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username=user, password=password)
    instance_ip = tools.get_localhost_ip()
    rs = client.add_naming_instance(service_name="test-service", ip=instance_ip, port=18082,
                                    cluster_name="DEFAULT")
(venv) [root@hmy test-service]# cd /app/flask_project/api/app/manage.py
from app import create_app
from flask_apscheduler import APScheduler
from app.common.apschedulerjob import nacos_healthy

# 创建app
app = create_app()

scheduler = APScheduler()
scheduler.init_app(app)

scheduler.add_job(func=nacos_healthy, trigger='interval', seconds=15, id="test-service")

scheduler.start()

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=18082)
posted @ 2022-10-12 16:38  醒日是归时  阅读(1291)  评论(0编辑  收藏  举报