Search code examples
pythonflaskhttp-headersflush

How to flush headers in Flask before sending HTTP 204?


I have a Flask REST service where one POST action may take a long time (for instance thirty seconds) before returning an HTTP 204 No Content.

The service is hosted on two servers acting as a failover, with Nginx deciding which server should be used, Nginx configuration being:

upstream demo-failover {
    server http-demo-primary.example.com weight=1000 max_fails=0;
    server http-demo-failover.example.com backup;
}

server {
    server_name demo.example.com;
    proxy_next_upstream error timeout invalid_header;
    proxy_connect_timeout 2;
    proxy_redirect off;
    proxy_read_timeout 240s;
}

When doing the concerned POST request, Nginx transmits it to http-demo-primary, and since it doesn't receive any response within the two seconds, abandons and does another request to http-demo-failover. Obviously, this is not suitable: since the first machine is up and running, Nginx should not failover to the second machine.

I imagine that sending headers—actually any header will do the trick—from the service will be enough for Nginx to understand that the machine is active and that it shouldn't failover to another one.

I know how to do it in PHP and ASP.NET. On the other hand, the only similar thing I can find in Flask is Streaming Contents section of the official documentation, which seems unsuitable in my case, where the response is HTTP 204.

How should I flush some headers in a context where the entity-body returned by the server is empty?


Solution

  • Having long-running HTTP requests is generally a bad idea. Instead, consider quickly returning an HTTP 202 Accepted response and then performing the long-running processing asynchronously. If the client needs to know when the request has completed, you can return a Location header with a URL that the client can poll for the status of the task.