Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten startup time for multiple workers #2693

Closed
joe42 opened this issue Nov 23, 2021 · 5 comments
Closed

Shorten startup time for multiple workers #2693

joe42 opened this issue Nov 23, 2021 · 5 comments
Assignees
Milestone

Comments

@joe42
Copy link

joe42 commented Nov 23, 2021

Hi,

When running our application with gunicorn with multiple workers I notice a considerable delay during startup.
All workers start in parallel and each worker takes an average of 5 sec to start.
What happens now with 8 workers is basically that the first worker completes its startup after roughly ~5*8 seconds instead of after 5 seconds.
Is there an easy way to make the worker startup sequential, like add a custom delay so that one of the workers can already start processing requests until the other workers are started? Or do I need to add some server hooks?

gunicorn 20.1.0
worker_class = 'gevent'
gevent 21.8.0
Python 3.9

@joe42
Copy link
Author

joe42 commented Nov 25, 2021

I am setting the variable preload_app = True in the configuration file now, which significantly reduces startup time of multiple workers. (See also: #1566)
However, this leads to an issue with gevent because the monkey-patching is done after the application code is loaded which is too late. That basically means all HTTPs requests inside the application fail, which is really bad.
So what you need to do is to manually call

from gevent import monkey
monkey.patch_all()

Before your application startup. I put it into the gunicorn config for now, so it looks something like this:

from gevent import monkey
monkey.patch_all()

worker_class = 'gevent'
preload_app = True

I still think starting the workers one by one would be a really nice feature if that is not possible at the moment.
However, I am happy with using the application reload.

It would be super nice if you could add that caveat in the configuration documentation for the preload parameter to reduce the amount of headaches in the world :)
Let me know if you want me to create a PR for that. I would be happy to help with that.

@benoitc
Copy link
Owner

benoitc commented Dec 11, 2021

I am setting the variable preload_app = True in the configuration file now, which significantly reduces startup time of multiple workers. (See also: #1566) However, this leads to an issue with gevent because the monkey-patching is done after the application code is loaded which is too late. That basically means all HTTPs requests inside the application fail, which is really bad. So what you need to do is to manually call

from gevent import monkey
monkey.patch_all()

Before your application startup. I put it into the gunicorn config for now, so it looks something like this:

from gevent import monkey
monkey.patch_all()

worker_class = 'gevent'
preload_app = True

never do that. You will unblock the arbiter loop which will result in bad supervision. Is your code using global variable that need to be used before your application is loaded?

@benoitc benoitc self-assigned this Dec 11, 2021
@jamadden
Copy link
Collaborator

never do that. You will unblock the arbiter loop which will result in bad supervision.

In our experience running a large production application and performing the gevent-monkey patch before even importing gunicorn, we have noticed no problems over the course of many years. cf #1566

@Auric-Manteo
Copy link

@benoitc I haven't experienced any issue with the workaround so far. I would be interested to know more about the issue you describe and discuss a better way forward like an alternative way to doing the application pre-load or starting the workers one by one.

@benoitc benoitc reopened this May 7, 2023
@benoitc benoitc added this to the 21.0 milestone May 7, 2023
@tilgovi tilgovi modified the milestones: 21.0, 22.0 Dec 28, 2023
@benoitc
Copy link
Owner

benoitc commented Aug 6, 2024

no activity since awhile. closing feel free to create a new ticket if needed.

@benoitc benoitc closed this as completed Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants