apw6
apw6

Reputation: 3

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

When run under Flask's local development server, jobs are added and run normally. When run under uWSGI, the job appears to get added to the job store but never is executed. A simple example with the described undesired behavior is given below:

__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

Upvotes: 0

Views: 2206

Answers (1)

Vinicius
Vinicius

Reputation: 36

Try adding this flag to your ini file:

lazy-apps=true

Similar issue: uWSGI lazy-apps and ThreadPool

Upvotes: 2

Related Questions