Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn with multiple workers: Is there an easy way to execute certain code only once?

If I start gunicorn with -w 4 with an app with the content

print 'hello'

it will print four times 'hello'. Is there some way to coordinate this in such a way that only one 'hello' is printed? I want to do some cleanup on start which I'd like to be only executed once.

like image 365
ben Avatar asked Jun 07 '14 23:06

ben


People also ask

Can Gunicorn handle multiple requests?

Gunicorn should only need 4-12 worker processes to handle hundreds or thousands of requests per second. Generally we recommend (2 x $num_cores) + 1 as the number of workers to start off with. From threads, The number of worker threads for handling requests.

How many workers can Gunicorn handle?

Each of the workers is a UNIX process that loads the Python application. There is no shared memory between the workers. The suggested number of workers is (2*CPU)+1 . For a dual-core (2 CPU) machine, 5 is the suggested workers value.

Is Gunicorn multithreaded?

If you use gthread, Gunicorn will allow each worker to have multiple threads. In this case, the Python application is loaded once per worker, and each of the threads spawned by the same worker shares the same memory space.


1 Answers

You can write a server hook, for example on_starting, which will run in the master process and thus only happen once.

I think these only work if you're using a Python script for configuration, like in their example.


(This is the OP speaking):

What worked for me was to create a config file "my_conf.py" with the content:

def on_starting(server):
     print(1)

Then

gunicorn [...] -w 2 -c my_conf.py &

prints "1" only once on server start although two workers were specified to be used.

like image 70
Eevee Avatar answered Sep 21 '22 17:09

Eevee