question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicating Startup Jobs

See original GitHub issue

When I add jobs after flask is started (through a req to the /scheduler), it behaves correctly, but somehow I cannot seem to get apscheduler to run only one instance of a job during startup. It creates two of each job regardless of whether I have max instances set to 1. At this point, I’m not sure this is a problem with my configuration or the code itself since this phenomena occurs whether I use gevent or not as either the flask engine or the scheduler.

# construct flask app object
from flask import Flask, request, session, jsonify, url_for, render_template
app = Flask(import_name=__name__)

# initialize logging and debugging
import sys
import logging
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.DEBUG)
app.config['ASSETS_DEBUG'] = False

# construct the landing page
@app.route('/')
def landing_page():
    return jsonify({'status':'ok'}), 200

# construct the catchall for URLs which do not exist
@app.errorhandler(404)
def page_not_found(error):
    return render_template('404.html'), 404

# construct scheduler object (with gevent processor)
from flask_apscheduler import APScheduler
from apscheduler.schedulers.gevent import GeventScheduler
gevent_scheduler = GeventScheduler()
ap_scheduler = APScheduler(scheduler=gevent_scheduler)

# add job to app scheduler
from time import time
scheduler_configs = {
    'SCHEDULER_JOBS': [ {
        'id': 'app.logger.debug.%s' % str(time()),
        'func': 'launch:app.logger.debug',
        'kwargs': { 'msg': 'APScheduler has started.' },
        'misfire_grace_time': 5,
        'max_instances': 1,
        'replace_existing': False,
        'coalesce': True
    } ],
    'SCHEDULER_TIMEZONE': 'UTC',
    'SCHEDULER_VIEWS_ENABLED': True
}
app.config.update(**scheduler_configs)

# attach app to scheduler and start scheduler
ap_scheduler.init_app(app)
ap_scheduler.start()

# initialize test wsgi localhost server with default memory job store
if __name__ == '__main__':
    from gevent.pywsgi import WSGIServer
    http_server = WSGIServer(('0.0.0.0', 5001), app)
    http_server.serve_forever()
    # app.run(host='0.0.0.0', port=5001)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
eayin2commented, Nov 15, 2016

I use app.run(use_reloader=False) to prevent flask to create two instances of flask_apscheduler.APScheduler. If I omit use_reload=False jobs are also fired twice for me. See: http://stackoverflow.com/a/15491587

0reactions
rj919commented, Nov 15, 2016

Separating the init from the launch did the trick. And… you make a good point about putting all the configs into an init file being better practice (especially for all these self-referential callables). Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Copy.ai Jobs | Wellfound (formerly AngelList Talent)
Copy.ai is hiring! See 5 jobs in December 2022! Apply to the latest jobs with a single profile and get in touch with...
Read more >
Jobs at Replicate (W20) | Y Combinator's Work at a Startup
When starting this company, we thought: instead of getting a job at the best place to work, let's make that best place to...
Read more >
Why do so many job sites duplicate the same jobs claiming ...
Everyone wants earn money, hence they usually post same jobs. What the users get is duplicated job openings. I also have observed the...
Read more >
How to remove duplicate entries in Startup of SYSCONFIG???
I have duplicate entries of the following startup items in the Startup tab of System Configuration Utility, ... Good job, PJ!
Read more >
How can I remove a duplicate startup entry? - Super User
Take a look with Autoruns - it might be in both a user-specific and system-wide startup location. You could also manually go through...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found