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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With