flask項目中添加定時任務

需求:項目中須要天天凌晨跑定時任務(插入數據到數據庫中)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上面跑APSchedler的定時任務不能執行

須要修改uwsgi配置文件,添加以下兩句flask

enable-threads = true
mule = /root/www/flask_idiom/apschedulerjob.py------這裏填寫定時任務文件的地址
複製代碼
相關文章
相關標籤/搜索