python 烧瓶 before_first_request_funcs

Sid*_*vij 1 python flask python-3.x

在我的 python3 Flask 应用程序中,我想在第一个请求之前执行几个重复任务。为了实现这一目标,我想利用@app.before_first_request_funcs.

谁能给我一个用法示例吗@app.before_first_request_funcs

这是我的示例代码:

import threading
import time

from flask import Flask

app = Flask(__name__)


def activate_job():
    def run_job():
        while True:
            print("recurring task")
            time.sleep(3)

    thread = threading.Thread(target=run_job())
    thread.start()


def activate_job2():
    def run_job2():
        while True:
            print("recurring task2")
            time.sleep(3)

    thread = threading.Thread(target=run_job2())
    thread.start()


@app.after_first_request(activate_job())
@app.before_first_request(activate_job2())
@app.route('/')
def home():
    return {"action": "This has done something"}


if __name__ == '__main__':
    print(app.before_first_request_funcs)
    app.run()
Run Code Online (Sandbox Code Playgroud)

dej*_*dej 7

正如 Damien 在评论部分所述,app.before_first_request装饰器从 2.3 开始bp.before_app_first_request被删除