f3030
f3030

Reputation: 11

APScheduler does not run scheduled tasks: Flask + uWSGI

I have an application on a Flask and uWSGI with a jobstore in a SQLite. I start the scheduler along with the application, and add new tasks through add_task when some url is visited. I see that the tasks are saved correctly in the jobstore, I can view them through the API, but it does not execute at the appointed time. A few important data:

uwsgi.ini

processes = 1
enable-threads = true

__init__.py

scheduler = APScheduler()
scheduler.init_app(app)
with app.app_context():
    scheduler.start()

main.py

            scheduler.add_job(
                id='{}{}'.format(test.id, g.user.id),
                func = pay_day,
                args = [test.id, g.user.id],
                trigger ='interval',
                minutes=test.timer
            )

in service.py

def pay_day(tid, uid):
    with scheduler.app.app_context():
     *some code here*

Interesting behavior: if you create a task by going to the URL and restart the application after that, the task will be executed. But if the application is running and one of the users creates a task by going to the URL, then this task will not be completed until the application is restarted. I don't get any errors or exceptions, even in the scheduler logs. I already have no idea how to make it work and what I did wrong. I need a hint.

Upvotes: 0

Views: 1615

Answers (1)

Elie Saad
Elie Saad

Reputation: 604

From the APScheduler FAQ:

uWSGI employs some tricks which disable the Global Interpreter Lock and with it, the use of threads which are vital to the operation of APScheduler. To fix this, you need to re-enable the GIL using the --enable-threads switch. See the uWSGI documentation for more details.

I know that you had enable-threads = true in uwsgi.ini, but try the to enable it using the command line.

Upvotes: 0

Related Questions