Delay Gunicorn until Flask is fully loaded?

Viewed 457

I'm deploying a Flask API with Gunicorn on Google Cloud Run. The issue I'm having is that my app has to load two large Pandas dataframes (this is non negotiable), which takes a few seconds. The issue I'm having is that Cloud Run will start driving traffic to the container as soon as the $PORT is listening, which means the first several requests get queues up until Flask is ready. I've tried using Docker's health check to ensure that the container status is not healthy until Flask is ready, but Cloud Run ignores that.

Question

How can I go about delaying Gunicorn's binding to the listening port?

I've tried running Gunicorn with the app factory pattern as well as the more common way, and that did nothing. I'm currently lazy loading the dataframes, which helps, but the best I'm able to do is 2 seconds. Hopefully the answers won't be related to optimizing the loading of my app, but rather, how to keep Cloud Run from delivering traffic to the container until I'm ready for it.

Edit

Using the --preload flag seems to do the trick, except that Cloud Run still drives traffic to the container before it's ready.

1 Answers

You can use gunicorn server hooks to define on_starting and on_exit. See https://github.com/benoitc/gunicorn/issues/2136 for info.

Alternatively, simply having a config file using --config and doing an import from there would cause your module to run (the initialising code in the module) which may be enough for you.

Related