Reputation: 2219
Gunicorn allows configuring a timeout for requests, as demonstrated in their documentation below. This seems to be a global configuration for the entire application.
Is it possible to configure different timeouts for different endpoints? Perhaps overriding the default timeout on url endpoints that are known to take a long time?
http://docs.gunicorn.org/en/stable/settings.html#timeout
timeout
-t INT, --timeout INT
30
Workers silent for more than this many seconds are killed and restarted.
Generally set to thirty seconds. Only set this noticeably higher if you’re sure of the repercussions for sync workers. For the non sync workers it just means that the worker process is still communicating and is not tied to the length of time required to handle a single request.
Upvotes: 27
Views: 2523
Reputation: 430
I dont know this is a good solution or not. But I did something like this:
pip install gevent
# gunicorn_config.py
import gevent
from gunicorn.workers.ggevent import GeventWorker
ADMIN_TIMEOUT = 30 # second
API_TIMEOUT = 5
class CustomWorker(GeventWorker):
def handle_request(self, listener_name, req, sock, addr):
try:
if req.path.startswith('/admin'):
timeout = ADMIN_TIMEOUT
else:
timeout = API_TIMEOUT
with gevent.Timeout(timeout):
super().handle_request(listener_name, req, sock, addr)
except gevent.GreenletExit:
pass
except SystemExit:
pass
except gevent.timeout.Timeout:
print(f'timeout for {req.path}')
raise StopIteration()
gunicorn --bind 0.0.0.0:8000 MyProject.wsgi -k 'gunicorn_config.CustomWorker'
Upvotes: 0
Reputation: 91
There is no easy way to do what you want to do. Probably the best option is to package each endpoint into a separate application, and then launch them with their own separate gunicorn processes / workers with the appropriate timeouts. Then put something like nginx to proxy the endpoints to different gunicorn processes.
Upvotes: 2