-2

I was looking at using APScheduler BackgroundScheduler to run a job every few seconds.

My question is if I put the Scheduler in my code, run it on the cloud and have thousands of users making requests,will the work of the scheduler be repeated?

To me it seems that for every process that Flask creates there will be a BackgroundScheduler thread running. Which means that on the cloud the work will be repeated.

If this is the case, is there a way to get around the issue without having to run some script in a separate machine. I would prefer to have the job in Flask mainly because of costs and maintainability.

J. Doe
  • 27
  • 1
  • 3
  • (a) How are you hosting the application? (b) What possible reason could you have to run a job "every few seconds"? (c) What happens if your job takes more than "a few seconds" to finish running? – ChrisGPT was on strike Feb 15 '20 at 01:37
  • @Chris (a) pythonanywhere (b) I need to update a database collection with info from other collection. It needs to be this fast because it has to do with location of users. (c) If you mean that it starts a job and doesn't finish it before another start, then I can only assume a database queue of updates will start building. – J. Doe Feb 15 '20 at 01:41
  • You're using PythonAnywhere for production hosting? Is it meant for that? I always thought it was a toy Python playground. – ChrisGPT was on strike Feb 15 '20 at 02:41

1 Answers1

1

From an architecture perspective, what you want is some operations to be done on a database. Ideally, this should not be a part of your Flask application.

Flask is meant to be a webserver. It should be used just like a webserver. The task that you want to run should be run as a separate script using ApScheduler or anything else that you might need. There needs to be a separation between the two as they are not dependant on each other.

Assume that tomorrow your Flask application crashes, does it mean that your script should not run?

If your requirement would have been something like an endpoint where you get the time and task to schedule and then you need to be processed at that time, having APScheduler and Flask together would've made sense.