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

Multiple instances of shceduler problem #51

Closed
shr-chauhan opened this issue Aug 16, 2017 · 5 comments
Closed

Multiple instances of shceduler problem #51

shr-chauhan opened this issue Aug 16, 2017 · 5 comments
Labels

Comments

@shr-chauhan
Copy link

I had a doubt, about using flask-apscheduler in a web app
When used in any forking web server (gunicorn uwsgi, etc...), processes get forked, thus apacheduler gets instantiated (and possibly started) in each forked worker process. This leads to the "many jobs running" issue?
In production multiple instances of the app gets created, it will initiate that many scheduler instances also right? so all those instances will try executing the job is it?
Am I wrong here?

@viniciuschiele
Copy link
Owner

yeah, your assumption is right, it will start one scheduler for each forked process and it will run the same job multiple times.
If you are running this service on multiple servers, you could keep only one active and the others as idle,
we achieved it using uwsgi legion.

@everhopingandwaiting
Copy link

            if os.environ.get("scheduler_lock") == "1":
                scheduler.start()
                logging.debug("Schedular Started ,-------------")
                os.environ["scheduler_lock"] = os.environ.get("scheduler_lock") + "1"

before start the program ,you had better to export scheduler_lock="1"

@shr-chauhan
Copy link
Author

Sorry for the late update
@everhopingandwaiting yea that is one way of doing it, but I am doing it differently
Running scheduler as a rpc server outside the app, so its a different process now....so whenever there is a scheduling request, it gets connected to the rpc and process the request there....

@viniciuschiele closing this

@everhopingandwaiting
Copy link

screenshot_2017-09-02-13-06-26-788_com sonelli juicessh

@viniciuschiele @viniciuschiele fcntl mscvrt

@chenxuewen
Copy link

Why did you add this code? Unable to dynamically modify timing tasks?

import atexit
import fcntl
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_MISSED, EVENT_JOB_EXECUTED
def scheduler_init(app):

f = open("scheduler.lock", "wb")
try:
    fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)


    scheduler.add_listener(job_listener, EVENT_JOB_ERROR | \
                       EVENT_JOB_MISSED | \
                       EVENT_JOB_EXECUTED)
    scheduler.init_app(app)
    scheduler.start()
except:
    pass
def unlock():
    fcntl.flock(f, fcntl.LOCK_UN)
    f.close()
atexit.register(unlock)

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

No branches or pull requests

4 participants