Flask ApScheduler Quick Guide under Python in detail

  • 2021-01-25 07:43:41
  • OfStack

Introduction: Flask is a very popular framework for Web development in the Python community. This article will attempt to introduce APScheduler as an application to Flask.

1. Flask is introduced

Flask is the well-known "microframework" in the Python community. It is based on a simple core and uses extension to add other features. It provides a very rich and easy to use extension pack.

Such as:

2. Flask-APScheduler

The community provides a module for Flask-APScheduler, so that people can use APScheduler directly in the Flask module. The command about installation is still used

pip to:

 >> pip install Flask-APScheduler

3. How to use Flask-APScheduler?

On how to use, direct code demo:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
Created on Mon Apr 17 22:27:34 2017
@author: bladestone
from flask_apscheduler import APScheduler
from flask import Flask
import datetime
class Config(object):
  JOBS = [
        'args': '',
        'trigger': {
          'type': 'cron',
          'second': '*/5'
app = Flask(__name__, static_url_path='')
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")
  # it is also possible to enable the API directly
  # scheduler.api_enabled = True

Code description:

We first use an Config object to wrap the APScheduler configuration information, and then read the configuration information through app.config.from_object(). scheduler.init_app(app) is initialized into app, and finally the operation of scheduler is started.

Similar Scheduler configurations are as follows:

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

The Scheduler is scheduled every 10 seconds.

More about flask - apscheduler example code can access: https: / / github com viniciuschiele/flask apscheduler/tree/master/examples

4. To summarize

flask-apscheduler from the positioning, just APScheduler into Flask acceptable mode, so as to carry out the task scheduling processing, the main scheduling operation or need to refer to APScheduler to carry out.

Related articles: