I use request.body
to get AJAX request data from jQuery to my django app, but sometime request.body
cause a Dyno timeout, i guess it's because of the user droping the connection and django keep waiting for the user's request, the exception look like this:
File "/app/myapp/views.py", line 271, in proccess_api File "/app/.heroku/python/lib/python2.7/site-packages/django/http/request.py", line 233, in body File "/app/.heroku/python/lib/python2.7/site-packages/django/http/request.py", line 292, in read File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 51, in read File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 45, in _read_limited File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.60.0.46/newrelic/api/web_transaction.py", line 780, in read File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/body.py", line 212, in read File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/body.py", line 128, in read File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/unreader.py", line 38, in read File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/unreader.py", line 65, in chunk File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 176, in handle_abort
The line in proccess_api:
req_data = json.loads(request.body)
I'm using Django 1.8.11 on Heroku, the exception is recorded using newrelic.
My questions are:
- How to handle this exception?
- Is it possible to set a timeout for
request.body
so the request won't block the gunicorn worker?
1 Answers
Answers 1
We don't have much info from you, but based on what you posted...
"How to handle this exception?" Wrap req_data = json.loads(request.body)
in a try
/except
like so:
try: req_data = json.loads(request.body) except: # Handle how you like # Maybe return a response 404 or 500?
It's even better if you can tell except
what particular exceptions to look out for:
try: req_data = json.loads(request.body) except ValueError: # Handle value errors except TypeError: # Handle type errors
Hard to say more without the full error output (all the stuff after File ...
) telling us what exact error Python threw. More about error handling here.
"Is it possible to set a timeout for request.body
so the request won't block the gunicorn worker?"
I think this isn't a timeout problem. Clearly the URL linked to that view is getting some sort of request
and tries to read its body
. Based on the error output you gave us, read
in the request
module is what throws the error. That's why your code is stopping. The fact that the request
body is unreadable means you should wrap it in a try
/except
as mentioned above.
Also, an adjustment that my help you is: req_data = json.loads(request.body.read())
.
0 comments:
Post a Comment