I have a streamlit app (built in docker, deployed to heroku) that formulates an optimization (a linear program) and then sends the schedule to a gurobi server. However, the process takes a while, and thus for a 20-year optimization at the hourly level, I chunk it into years (8760 data points for a year, x20 years).
Basically, once the inputs are set by the user and saved as state variables a la Joel Grus’ Game State hacks , the process is:
- Formulate inputs and constraints to optimization for 2021 →
- send to gurobi →
- get back optimal solution from gurobi as a dataframe->
- display on Streamlit app “Optimal Solution for 2021 Complete! Moving on to 2022…”
- Repeat for next iteration, for 20 iterations.
- Concatenante all 20 dataframes from the process and display some summary results and some download and “Send to database” buttons for the results.
Each iteration takes about a minute. However, I can only make it to about the 10th iteration (sometimes less, sometimes more) of that before the streamlit frontend “disconnects” from the running loop process of the app and resets all of my state variables, etc, like I had just reloaded the page. The background app process still continues (it’s still looping through the iterations - it doesn’t know streamlit got disconnected), but there is no way to reconnect to the running process again. Without streamlit, this process is fine to run locally with scripts, but with streamlit the frontend is so unreliable as to be unusable for this app. Nothing more frustrating than to get nearly done with a process that took 20 minutes and then it resets itself, forcing the user to start the process over.
I’m really not sure what to try at this point, other than build something much more robust like a flask app that sends these jobs to a job container, which then sends back the result when done. Running the optimization on the same computer as the frontend seems like a fool’s errand.
Anyone have tips to keep a streamlit frontend connected to a long-running process without resetting?