Scale to 0 python/flask server

Does anyone know of a scale to 0 python/flask server example? Fly specific or even maybe generic example would be helpful.

I’m not as familiar with the python ecosystem but need to run some very python specific code, so something like ‘gracefully shut a flask server down if it hasn’t received requests in the last 5 minutes’ has proven somewhat non-trivial.

I haven’t seen one for Flask directly, but there’s a Datasette plugin that might be portable: datasette-scale-to-zero/ at main · simonw/datasette-scale-to-zero · GitHub

1 Like

In addition to what Kurt suggested:

Not graceful shutdown but, tiered_proxy will go away after a preset idle period has elapsed (default is 120s).

  1. Setup tiered_proxy and entrypoint in your Dockerfile like so: fastify-functions/Dockerfile at 0f4ef94451f5298a999410f94405c4fb739df105 · fly-apps/fastify-functions · GitHub
  2. Set idle timeout as required, then start flask (instead of node): fastify-functions/ at main · fly-apps/fastify-functions · GitHub

We run a node app where we try to gracefully shut it all down, and you’re right, it isn’t straight-forward (commit). Perhaps, a fly.toml entry for sigint_idle_threshold = 300s would be nice to have out of the box.

1 Like

Yeah that would be super simple.

Thank you for the ideas! I will look into them.

1 Like