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?

nicknaky
  • 11
  • 2

1 Answers1

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.

Alex Grönholm
  • 5,563
  • 29
  • 32
  • 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? – nicknaky Mar 18 '16 at 14:42
  • 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. – Alex Grönholm Mar 20 '16 at 12:44