[It was a specification of curl & HTTP] When Flask with uWSGI is run by HTTP, there is a clean delay of 1 second when posting a file.

Twitter Let's start Qiita to commemorate the reincarnation.

~~ As the title says, when Flask is run with uWSGI @ HTTP, I have confirmed that there is a beautiful delay of 1 second when posting a file, so I will introduce the reproduction procedure. ~~

This is a specification of curl that issues a request with the ʻExpect: 100-continue` header and waits 1 second to ask if you want to post the file to the server. It seems. [^ 1]

$ curl -v -s -XPOST -F file=@LICENSE localhost:5002
* Rebuilt URL to: localhost:5002/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 5002 (#0)
> POST / HTTP/1.1
> Host: localhost:5002
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 1271
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------ebc5516179fb0a6c
>
* Done waiting for 100-continue
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 20
<
* Connection #0 to host localhost left intact
Took: 0:00:01.011157

It is said to be HTTP / 1.1 compliant. [^ 2]

uWSGI supports the handling of ʻExpect: 100-continue`, and it seems that you should add the following line to the ini file. [^ 1]

http-manage-expect = 1

Experimental settings

https://github.com/akeyhero/uwsgi-http-is-slow

As per the repository above

  1. When Flask is started independently
  2. When Flask is started via uWSGI with HTTP settings
  3. When Flask is started via uWSGI with uwsgi protocol settings

I am conducting an experiment on.

1. When Flask is started independently

simply

$ flask run --host=0.0.0.0

When started with.

2. When Flask is started via uWSGI with HTTP settings

uWSGI

http = 0.0.0.0:80

When started with settings that include.

3. When Flask is started via uWSGI with uwsgi protocol settings

uWSGI

socket = /socket/uwsgi.sock

When started with settings that include.

version

Flask==1.1.1
uwsgi==2.0.18

Experimental result

The response times when POSTing files using curl are as follows. Here, as long as it is confirmed, the file can be anything, so I am using the LICENSE file in the repository.

Response time
1.Flask alone 0.067
2. Flask w/ uWSGI @ HTTP 1.063
3. Flask w/ uWSGI @ uwsgi 0.062

You can see that the response time is just one second slower only when Flask is started with uWSGI @ HTTP.

This time, we have posted the data for one trial, but the response time will be almost the same no matter how many times you try.

in conclusion

~~ Flask alone does not cause this phenomenon, so I threw issue appropriately because uWSGI was probably the cause. However, I'm not sure if it's because of Flask, or if it's caused by a single combination technique. ~~

~~ If you have any details, please let us know. : bow: ~~

I was taught by a contributor of uWSGI. [^ 1] I learned a lot.

Recommended Posts

[It was a specification of curl & HTTP] When Flask with uWSGI is run by HTTP, there is a clean delay of 1 second when posting a file.
It was a little difficult to do flask with the docker version of nginx-unit
[Shell art] Only when it is a multiple of 3 and a number with 3 becomes stupid