1

I'm using Falcon 1.4.1 and Gunicorn 19.9.0 inside docker.

Having trouble figuring out the best way to initialize the application - running some code once when my REST API is started instead of once per worker. I have 3 or more workers running for my application.

I've tried using the gunicorn on_starting webhook, but it still ran once per worker. In my gunicorn_conf.py file:

def on_starting(server):
    print('Here I am')

I also tried the gunicorn preload_app setting which I'm happily using in production now and which does allow application initialization to run once before it starts the workers.

I want to be able to use the gunicorn reload setting so file changes restart the application which directly conflicts the with preload_app setting.

May just want too much :) Anyone have any ideas on solutions? I saw some attempts to get a lock file with multiprocessing, but turns out you get a lockfile/worker.

William
  • 705
  • 1
  • 6
  • 17
  • If your issue with `preload_app` is only while developing, could you not just use a single worker then and only switch to multiple workers in prod? – Pybe Nov 13 '18 at 15:20
  • Tried that, but I've set it up so that the API called the API and it locked up since the current process was blocked when calling the API with just one worker. There is probably a better way of handling things, but I'm trying to make a library that powers the API, is usable on its own, and trying to keep it as DRY as possible. – William Nov 15 '18 at 14:12

1 Answers1

0

I am not able to understand properly what exactly you want to achieve? It will help if you post error code also.

As you mention you are able to run your code once using Gunicorn preload_app setting instead of for all worker.

Now you can reload Gunicorn instances on file change using following code:

gunicorn --workers 3 -b localhost:5000 main:app --reload

If this is not what you are looking for then share error code here as you mention that "I saw some attempts to get a lock file with multiprocessing, but turns out you get a lockfile/worker." I will try my best to help you.

Harshad Kavathiya
  • 8,939
  • 1
  • 10
  • 19
  • Using --reload option is not compatible with the preload_app setting. The preload_app setting runs my setup code once. The --reload setting runs my setup/initialization code once for every gunicorn worker I'm running (4 or more). I haven't figured out a robust way to run my initialization code once without using preload_app which makes development painful (have to restart the gunicorn server every time I make a chance). Thanks for taking a look at this! – William Aug 02 '18 at 16:17
  • This is the issue I saw trying to use a multiprocessing lock for gunicorn: https://stackoverflow.com/questions/18213619/sharing-a-lock-between-gunicorn-workers?rq=1 without using preload - the Lock isn't shared between workers. – William Aug 02 '18 at 16:19