How to perform periodic task with Flask in Python

前端 未结 3 1282
春和景丽
春和景丽 2020-12-14 08:28

I\'ve been using Flask to provide a simple web API for my k8055 USB interface board; fairly standard getters and putters, and Flask really made my life a lot easier.

<
3条回答
  •  臣服心动
    2020-12-14 08:33

    For my Flask application, I contemplated using the cron approach described by Pashka in his answer, the schedule library, and APScheduler.

    I found APScheduler to be simple and serving the periodic task run purpose, so went ahead with APScheduler.

    Example code:

    from flask import Flask
    
    from apscheduler.schedulers.background import BackgroundScheduler
    
    
    app = Flask(__name__)
    
    def test_job():
        print('I am working...')
    
    scheduler = BackgroundScheduler()
    job = scheduler.add_job(test_job, 'interval', minutes=1)
    scheduler.start()
    

提交回复
热议问题