国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解Python下Flask-ApScheduler快速指南

2020-02-15 23:30:44
字體:
供稿:網(wǎng)友

引言:Flask是Python社區(qū)非常流行的一個(gè)Web開發(fā)框架,本文將嘗試將介紹APScheduler應(yīng)用于Flask之中。

1. Flask介紹

 Flask是Python社區(qū)大名鼎鼎的"microframework",基于簡單的核心,使用extension來增加其他功能,其提供非常豐富易用的擴(kuò)展包,

比如:

2.  Flask-APScheduler

社區(qū)提供了一個(gè)Flask-APScheduler的模塊,方便大家直接在Flask模塊中使用APScheduler。 關(guān)于安裝的命令,仍然是使用

pip來進(jìn)行:

 >> pip install Flask-APScheduler

3.  如何使用Flask-APScheduler?

關(guān)于如何使用,直接代碼演示:

#!/usr/bin/env python2# -*- coding: utf-8 -*-"""Created on Mon Apr 17 22:27:34 2017 @author: bladestone"""from flask_apscheduler import APSchedulerfrom flask import Flaskimport datetime class Config(object):  JOBS = [      {        'id':'job1',        'func':'flask-ap:test_data',        'args': '',        'trigger': {          'type': 'cron',          'day_of_week':"mon-fri",          'hour':'0-23',          'minute':'0-11',          'second': '*/5'        }        }    ]      SCHEDULER_API_ENABLED = True app = Flask(__name__, static_url_path='') @app.route("/")def hello():  return "hello world"  def test_data():  print("I am working:%s" % (datetime.datetime.now())) if __name__ == '__main__':  scheduler = APScheduler()  print("Let us run out of the loop")  app.config.from_object(Config())   # it is also possible to enable the API directly  # scheduler.api_enabled = True  scheduler.init_app(app)  scheduler.start()   app.run(debug=False)

代碼說明:

這里首先使用了一個(gè)Config對(duì)象來包裝APScheduler的配置信息,然后通過app.config.from_object()的方式,讀取配置信息。 基于scheduler.init_app(app)初始化到app中,最后啟動(dòng)scheduler的操作。

類似的Scheduler的配置還有如下:

 JOBS = [    {      'id': 'job1',      'func': 'jobs:job1',      'args': (1, 2),      'trigger': 'interval',      'seconds': 10    }  ]

這個(gè)Scheduler是每隔10秒進(jìn)行調(diào)度一次。

更多的關(guān)于flask-apscheduler的示例代碼可以訪問:https://github.com/viniciuschiele/flask-apscheduler/tree/master/examples

4. 總結(jié)

flask-apscheduler從定位上講,只是將APScheduler轉(zhuǎn)換為了Flask可以接受的方式,從而進(jìn)行任務(wù)的調(diào)度處理,主要的調(diào)度操作還是需要參照APScheduler來進(jìn)行的。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林站長站。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉水县| 城固县| 凌源市| 大渡口区| 尉犁县| 招远市| 越西县| 陆河县| 云和县| 合水县| 福州市| 庆阳市| 米易县| 基隆市| 陵川县| 含山县| 通海县| 镇康县| 邓州市| 微博| 鄯善县| 普格县| 乐安县| 卓资县| 巴楚县| 高台县| 广德县| 高州市| 清徐县| 绥阳县| 太保市| 滨州市| 镇坪县| 曲麻莱县| 颍上县| 从江县| 廊坊市| 汶川县| 通化县| 新田县| 米林县|