Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uWSGI keepalive

Is it possible somehow to pass through the keepalive limitation of uwsgi? If not, what is the best way of persistent connection implementation. I'm using NGiNX + uWSGI (Python), and I want clients to have asynchronous updates from server.

like image 949
ZeAL0T Avatar asked Mar 22 '12 13:03

ZeAL0T


People also ask

What is uWSGI used for?

uWSGI is an open source software application that "aims at developing a full stack for building hosting services". It is named after the Web Server Gateway Interface (WSGI), which was the first plugin supported by the project. uWSGI is maintained by Italian based software company unbit.

What is Post buffering in uWSGI?

Post-buffering mode (uWSGI >= 2.0. This means that as soon as the uwsgi packet (read: the request headers) is parsed, it is forwarded to the backend/backends. Now, if your web-proxy is a streaming-one too (like apache, or the uWSGI http router), your app could be blocked for ages in case of a request with a body.

What is uWSGI socket?

uWSGI includes an HTTP/HTTPS router/proxy/load-balancer that can forward requests to uWSGI workers. The server can be used in two ways: embedded and standalone. In embedded mode, it will automatically spawn workers and setup the communication socket.

How do I know if uWSGI is running?

Check the status of the uwsgi process to find out whether it was able to start: sudo systemctl status uwsgi.

How do I set up keep-alive with uWSGI?

Also remember to set “Connection: Keep-Alive” in your response. You can automate that using the add-header = Connection: Keep-Alive option. Since uWSGI 2.1 (master branch) you can use the http11-socket option. http11-socket may replace the add-header and http-keepalive options (but it doesn’t touch tcp stuff as so-keepalive does).

What is the use of timeout in uWSGI?

The timeout is set only between two successive read operations, not for the transmission of the whole response. If the uwsgi server does not transmit anything within this time, the connection is closed. This directive appeared in version 1.7.11. Enables or disables buffering of a client request body.

How do I make uWSGI natively speak http?

The http-socket <bind> option will make uWSGI natively speak HTTP. If your web server does not support the uwsgi protocol but is able to speak to upstream HTTP proxies, or if you are using a service like Webfaction or Heroku to host your application, you can use http-socket.

How to enable gzip on uWSGI?

With the http-auto-gzip option, uWSGI can automatically gzip content if the uWSGI-Encoding header is set to gzip while Content-Length and Content-Encoding are not set. If you need a load balancer/proxy it can be a very good idea.


1 Answers

UWSGI supports keep-alive via --http-keepalive option if you receive requests via http.

/tmp$ cat app.py
def application(env, start_response):
    content = b"Hello World"
    start_response('200 OK', [
        ('Content-Type','text/html'),
        ('Content-Length', str(len(content))),
    ])
    return [content]

Run with:

/tmp$ uwsgi --http=:8000 --http-keepalive -w app &> /dev/null

And we can see connect calls via strace:

~$ strace -econnect wrk -d 10 -t 1 -c 1 http://127.0.0.1:8000 
connect(3, {sa_family=AF_INET, sin_port=htons(8000), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
Running 10s test @ http://127.0.0.1:8000
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    92.32us   56.14us   2.81ms   97.20%
    Req/Sec    11.10k   389.34    11.84k    68.32%
  111505 requests in 10.10s, 7.98MB read
Requests/sec:  11040.50
Transfer/sec:    808.63KB
+++ exited with 0 +++

See? Only one connection.

like image 169
bav Avatar answered Sep 20 '22 15:09

bav