Flask: timed task development and flask Development

Source: Internet
Author: User

Flask: timed task development and flask Development

 

I recently developed my interface testing platform, but met a requirement and needed to develop a scheduled task. Therefore, Baidu searched for a pFlask-APScheduler and started my first learning, thus,

The requirement is as follows:

1. Add a scheduled task,

2. Pause a scheduled task,

3. Restore scheduled tasks

4. Remove a scheduled task

5. Obtain the scheduled task ,.

Then my code can be written.

From flask import Flask, requestfrom flask_apscheduler import APSchedulerapp = Flask (_ name _) scheduler = APScheduler () class Config (object): JOBS = [] def task1 (a, B): print (str (a) + ''+ str (B) @ app. route ('/pause') def pausetask (id): # suspend scheduler. pause_job (id) return "Success! "@ App. route ('/resume') def resumetask (id): # restore scheduler. resume_job (id) return" Success! "@ App. route ('/gettask') def get_task (id): # Get jobs = scheduler. get_jobs () print (jobs) return '000000' def remove_task (id): # Remove scheduler. delete_job (id) return 111@app.route ('/addjob', methods = ['get', 'post']) def addtask (): schedtask. add_job (func = task1, id = '1', args = (1, 2), trigger = 'interval ', seconds = 5, replace_existing = True) return 'sucess 'if _ name _ =' _ main _ ': app. config. from_object ('config') scheduler. init_app (app = app) scheduler. start () app. run (debug = True)

In this way, we can test our scheduled tasks.

Add task

 

Console display

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.