APScheduler on a single EC2 instance getting called multiple times
Asked Answered
F

1

0

I have a Flask application that's deployed on a single AWS EC2 instance. In my __init__.py file I've instantiated a BackgroundScheduler with a job scheduled to run at every 1 hour interval. Here's an example of my __init__.py code:

application = Flask(__name__)
app = application
scheduler = BackgroundScheduler()
run_my_jobs = scheduler.add_job(my_job,  'interval', hours=1)
scheduler.start()

I would assume that since the instantiation is done outside of the Flask context, and with only one single instance running on EC2, that my scheduler should only be instantiated once, regardless of how many users are connected to my Flask app throughout the day.

That has generally been the case for the past couple months, however recently in the past couple days I noticed the scheduler has been executing the job almost 2-3 times per hour. While I've been continuing to push code to production, the __init__.py file has remained unchanged so I'm confused as to what are possible reasons to cause multiple instantiations of the scheduler?

Formation answered 17/3, 2016 at 18:30 Comment(0)
A
0

There are generally two ways this can happen:

  1. Using more than one worker process
  2. Putting the code in the module that is your application's entry point and not protecting it with an if __name__ == '__main__': block

From your description I assume #1 is not the case, so I'd go for #2.

Award answered 18/3, 2016 at 7:32 Comment(2)
So the code lies in the _init_.py file of my app module. And then in a separate file outside of the app module/directory, is an application.py file that does contain the if _name_ == "_main_": block where I have application.run(), and with the application object being imported in earlier from app. What would be the correct way to set this up, given that the requirements to deploy on EC2 would be through an application.py file in the root directory, while my app module is inside the app folder?Formation
I have no experience with EC2 but generally imports should not have side effects. Put the scheduler setup code in a function and import&call that function from your application.py.Angell

© 2022 - 2025 — McMap. All rights reserved.