gpt4 book ai didi

python - 处理 request.body 超时异常

转载 作者:行者123 更新时间:2023-11-28 19:13:37 25 4
gpt4 key购买 nike

我使用 request.body 从 jQuery 获取 AJAX 请求数据到我的 django 应用程序,但有时 request.body 导致 Dyno 超时,我想这是因为用户断开连接,django 继续等待用户的请求,异常如下所示:

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

process_api 中的行:

req_data = json.loads(request.body)

我在 Heroku 上使用 Django 1.8.11,使用 newrelic 记录异常。

我的问题是:

  • 如何处理这个异常?
  • 是否可以为 request.body 设置超时,这样请求就不会阻塞 gunicorn worker?

最佳答案

看起来这是一个 design limitation in gunicorn :

Gunicorn is designed to be used behind a buffering reverse proxy

Gunicorn uses a pre-forking process model by default. This means that network requests are handed off to a pool of worker processes, and that these worker processes take care of reading and writing the entire HTTP request to the client. If the client has a fast network connection, the entire request/response cycle takes a fraction of a second. However, if the client is slow (or deliberately misbehaving), the request can take much longer to complete.

关于python - 处理 request.body 超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36385944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com