-1

I'm a little new to the Flask world and I'm wondering how to best query a database every second or so in the background from a Flask application. I want to be able to run a query, then grab that data and do something with it if it meets certain criteria, but I'm just a little hung up as to where to place the code in my Flask application, so that it's always running.

Nick Bitzer
  • 71
  • 3
  • 12

1 Answers1

1

What's your use case? My guess is you're trying to query the database ahead of time so the data you want is ready by the time a web request comes in.

If I understand Flask correctly, it doesn't inherently support background tasks. Everything's based on web requests. If you do a Google search for "flask background task" you'll find that Flask docs recommend you use Celery for background workers. You don't necessarily have to use Celery as there are a number of systems that can do this. Celery supports periodic tasks too. You don't even necessarily need Celery to do this; you can write your own daemon in Python to wake up every second and query the database.

One question is what are you going to do with the data from the query? Do you need to supply it to a Flask web request? If so, you're gonna have to somehow stash the data in a place that can be retrieved from your Flask handler. A common way to do this is memcached or some other caching layer.

Now you might be thinking about trying to use Python threading or multiprocessing library from inside of your Flask app, or some other in-process scheduler, and then sharing the result via some global state. This probably won't work. Flask uses multiple processes/threads to handle incoming requests, and you never really know which process you're gonna land in. See Preserving global state in a flask application

Community
  • 1
  • 1
schematic
  • 1,964
  • 1
  • 16
  • 20