Flask slow at retrieving post data from request?

跟風遠走 提交于 2019-12-03 04:30:18

The flask development server is expected to be slow. From http://flask.pocoo.org/docs/deploying/:

You can use the builtin server during development, but you should use a full deployment option for production applications. (Do not use the builtin development server in production.)

As Marcus mentioned in the comments, another WSGI server like gunicorn or tornado would be much faster and more reliable, so definitely use one of those for deployment and benchmarking.

If you're worried about working quickly during development, you can use gunicorn in development just like you would in deployment. If you're deploying to heroku, for example, you can run "foreman start" and the gunicorn server will start right up.

I had this problem on a line like this, it was taking about 1.0 second! It's in a flask post handler:

username=request.form.get('username')

I was testing it with curl -F:

curl -F username="x" http://127.0.0.1:5000/func

I just changed -F to -d and it got 0.0004 seconds!!!

curl -d username="x" http://127.0.0.1:5000/func

I think flask has a problem to retrieving "multipart/form-data" content-type.

If you use curl to send a request, Expect: 100-continue might cause the behavior. I met a similar behavior with uwsgi, flask and curl. What happens in my case are the following:

  • If the request body size is larger than 1024 byte, curl posts data with Expect: 100-continue header.
  • However, uwsgi can't deal with the header. So the uwsgi doesn't respond 100-continue.
  • curl waits for the100-continue response until about one second time-out.

When curl sends 100-continue | Georg's Log was useful for me to know the curl behavior.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!