Skip to content

Set TCP_NODELAY for http.server #108736

Open

Description

@Stevenjin8

Feature or enhancement

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Proposal:

Python's default http.server should set the TCP_NODELAY flag to accepted connections.

The default python HTTP server (running python -m http.server) will write to the socket twice, once for the header https://github.com/python/cpython/blob/d48760b2f1e28dd3c1a35721939f400a8ab619b8/Lib/http/server.py#L771C13-L771C29

and once for the body

self.copyfile(f, self.wfile)

This causes HTTP transactions on long-lived connections to have a 40 ms delay due to how Delayed Acks and Nagle's Algorithm interact (and they are both enabled by default).

I tested this using fortio on a Ubuntu 20.04 VM.

python -m http.server -p http/1.1
# in another session
fortio load -qps -1 -c 1 http://localhost:8000/some-file

Another option would be to expose a top-level flag to disable Nagle's with the TCP_NODELAY. Curl does this.

Exposing a TCP flag on the top level API is kinda weird, but disabling it is pretty common behaviour. For example, Golang will disable it https://news.ycombinator.com/item?id=34179426

Activity

  1. ericvsmith commented on Aug 31, 2023

    @ericvsmith
    Member

    Does anyone use http.server in an environment where this makes a difference? It is, after all, not recommended for production use.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Aug 31, 2023
  3. Stevenjin8 commented on Oct 15, 2023

    @Stevenjin8
    ContributorAuthor

    Does anyone use http.server in an environment where this makes a difference? It is, after all, not recommended for production use.

    Sorry just saw this. I hope nobody is using this this for anything performance critical. That said, I've used it in my dev environment. Setting nodelay only saves around 50ms per request, but I figured that 50ms times however many times people use this could add up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions