2
votes

I have a Flask app (Plotly Dash) that fetches data from a DB which gets updated every day. Deployed on AWS, I run the app with gunicorn/nginx. Problem is, the data used in the app are from the latest gunicorn run, not the latest in the DB.

A solution I thought is adding an attribute to gunicorn command (eg. --reload) with a timer, to auto-restart gunicorn daily. Is this possible, or even, are there any other workarounds?

SOLUTION : https://community.plot.ly/t/how-to-refresh-data-on-fly/13069

1
Is modifying the application to restart itself after some time or restart on e.g. a trigger (incoming HTTP request on /service/restart for example) a possibility?syntonym
Re-running the script on page refresh would be ideal essentially.Vasilis
Can you pack the script in the flask route? How long does processing of the data take, is it just pulling something from the DB or is there some "longer" processing involved? Can you show some code of the flask route and of the DB connection?syntonym
The process is, - opening a connection with the db with psycopg2, - create a cursor, - use the cursor to run 4 separate queries. Data from the queries are stored in local pandas DataFrames, get aggregated based on specific needs and then visualized. Whole process might take up to 2 minutes. There is no Flask routing, I initialize a Flask(name) server which hosts a Plotly Dash appVasilis
Could you post your solution to as an answer to your own question (and accept it)? That way it's visible that this question has a solution and hopefully might help others.syntonym

1 Answers

1
votes

You can signal gunicorn to reload with the SIGHUP signal. To try it out manually you can do that e.g. with killall -s 1 gunicorn, to reload everyday you probably want to use something like cron or some AWS specific service. It seems like AWS has some documentation on that.

Using killall is suboptimal, as it operates on the name of the process. It can break if the name changes (e.g. gunicorn renames itself to be identified with the application name) or if there are more than one gunicorn running. But as long as this is the only usecase for the server, it should be fine.