如何使用 uwsgi + flask_apscheduler 通过请求将作业添加到 apscheduler

How to add jobs to apscheduler via request using uwsgi + flask_apscheduler

当运行在Flask本地开发服务器下,jobs被添加,运行正常。当在 uWSGI 下 运行 时,作业似乎被添加到作业存储但从未被执行。下面给出了一个具有所描述的不良行为的简单示例:

__init__.py

import flask
from datetime import datetime, timedelta
from flask_apscheduler import APScheduler

app = flask.Flask("apscheduler_test")
app.config["SCHEDULER_API_ENABLED"] = True
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()

def test_job():
    print("test job run")

@app.route("/test")
def apscheduler_test():
    print("Adding Job")
    scheduler.add_job(id="101",
                      func=test_job,
                      next_run_time=(datetime.now() + timedelta(seconds=10)))
    return "view", 200

if __name__ == '__main__':
    app.run(port=5050)

apschedule_test.ini

[uwsgi]
pidfile = /var/run/%n.pid
chdir = /opt/apscheduler
master = true
enable-threads = true
threads = 20
http-socket = :48197
logto = /var/log/%n.log
plugin = python3
module = %n
callable = app
processes = 1
uid = root
gid = root
daemonize = /var/log/apscheduler_test.log

尝试将此标志添加到您的 ini 文件中:

lazy-apps=true

类似问题: