Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting debug mode to "True" in Flask make the jobs run twice. #139

Closed
andresromerodev opened this issue Feb 20, 2021 · 3 comments · Fixed by #140
Closed

Setting debug mode to "True" in Flask make the jobs run twice. #139

andresromerodev opened this issue Feb 20, 2021 · 3 comments · Fixed by #140

Comments

@andresromerodev
Copy link

When setting up an app to run in debug mode as the snippet added below, flask-apscheduler will run each job scheduled with @scheduler.task('cron', ...) twice.

app.run(host='0.0.0.0', port=5000, debug=True)

To replicate add the following Job:

app = Flask(__name__)

scheduler = APScheduler()
scheduler.api_enabled = True
scheduler.init_app(app)
scheduler.start()

@scheduler.task('cron', id='do_job_for_testing', minute='25')
def job_for_testing():
    print('This statement will be printed twice')

Which will create the following output:

$ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 235-483-671
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
This statement will be printed twice
This statement will be printed twice
@mattl1598
Copy link

mattl1598 commented Feb 21, 2021

wait that's why all my functions are running twice?! I've been trying to write if statements to catch duplicate jobs but they run so quickly after the first one that I couldn't filter them out.

does removing debug=True fix it?

edit: just tested it and yes that does fix it as a workaround. there goes the last 5 days of troubleshooting

@viniciuschiele
Copy link
Owner

viniciuschiele commented Feb 21, 2021

That happens because Flask in debug mode spawns a child process so that it can restart that process each time your code changes, the new child process initializes and starts a new APScheduler, that's why your jobs are running twice.

Context: https://stackoverflow.com/questions/25504149/why-does-running-the-flask-dev-server-run-itself-twice

I guess I can detect if the current process is the child one and then I wouldn't allow APScheduler to start.

For now, to work around that, you can either to set Flask reloader use_reloader=False or debug=False.

@andresromerodev
Copy link
Author

Thanks @viniciuschiele the use_reloader works fine when running the app locally.
I also ran into the same when deploying the app to Heroku, even though I was setting use_reloader=False.

As I was using gunicorn to start the app and I found gunicorn also spawns a child process, so to avoid this from happen I just had to modify my Procfile and add:

web: gunicorn app:app --preload

I thought this could be useful for anyone deploying to a Cloud Platform using a WSGI like gunicorn with Flask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants