需求:项目中须要天天凌晨跑定时任务(插入数据到数据库中)html
apschedulerjob.pypython
from apscheduler.schedulers.blocking import BlockingScheduler
def func():
XXXXXX
scheduler = BlockingScheduler()
scheduler.add_job(func, 'cron',hour=0, minute=20)
scheduler.start()
## 下面这句加在定时任务模块的末尾...判断是否运行在uwsgi模式下, 而后阻塞mule主线程(猜想).
try:
import uwsgi
while True:
sig = uwsgi.signal_wait()
print(sig)
except Exception as err:
pass
复制代码
add_job后面的参数cron表明定时调度,具体参数:git
Parameters:
year (int|str) – 4-digit year
month (int|str) – month (1-12)
day (int|str) – day of the (1-31)
week (int|str) – ISO week (1-53)
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hour (int|str) – hour (0-23)
minute (int|str) – minute (0-59)
second (int|str) – second (0-59)
start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)
jitter (int|None) – advance or delay the job execution by jitter seconds at most.
复制代码
参考官方文档地址,能够按照需求修改参数数据库
须要修改uwsgi配置文件,添加以下两句flask
enable-threads = true
mule = /root/www/flask_idiom/apschedulerjob.py------这里填写定时任务文件的地址
复制代码