Uwsgi, a good implementation of the WSGI protocol, source code here: Https://github.com/unbit/uwsgi
C language writing, interested can be studied under.
On DEMO:
wsgi_server.py
def application (env, start_response): start_response (' kOK', [(' content-type'text/html')]) return'helloworld'
Application:
Deploy the above applications using UWSGI:
Uwsgi--http 0.0.0.0:9090-p 4-l 100-m-R 100000 -z30-l--wsgi-file wsgi_server.py --max-apps 65535--stats 127 .0.0.1:1717--post-buffering 100M--cpu-affinity--buffer-size 65535--daemonize/tmp/uwsgi--pidfile/tmp/uwsgi.pid --memory-report--threads 4
Then the browser accesses: http://localhost:9090/.
Advantage:
Increased concurrent access support (-P process count, number of--threads threads)
Improved service run stability (--daemonize)
Installation
Pip Install Uwsgipip Install Uwsgitop
Uwsgi--uwsgi Server
Uwsgitop--uwsgi Server Performance View tool, usage:
With the above example
Uwsgitop 127.0.0.1:1717
Parameter Detail Description
Official Document: Http://uwsgi-docs.readthedocs.io/en/latest/Options.html
Pick a few highlights:
--wsgi-file, specifying the WSGI entry file
- P , the number of workers, is also the number of processes, according to the Convention can be set by default to the number of cores, but is not the most need to see through the Uwsgitop (personally think uwsgitop no use).
--threads, the number of threads, the number of threads per process, the task of the process is done with the thread's pattern. As written in C, so do not worry about the Gil problem, but there is no thread on Linux, the thread is inherently pseudo-process (and there is a context switching cost), so it is not recommended.
(after the use of the UWSGITOP monitoring, you can use the keyboard "A" key to view the resource consumption of the thread)
--listen, the length of the kernel listener (listen) network queue is limited by the maximum number of network connections (net.core.somaxconn) of the file operating system, and the larger the length means that the less requests are lost in a highly concurrent environment.
--cpu-affinity, CPU-friendly, that is, the process does not switch cores at runtime (switching the time cost of the person)
--stats, the URL of the monitoring program, only set this parameter can be used uwsgitop 1717来 watch monitoring
--memory-report, open memory consumption report (as you can see in uwsgitop)
--master, start the main process, easy to manage all processes, can be used in conjunction with Pidfie. Convenient Stop (Uwsgi--stop/tmp/uwsgi.pid)/restart Uwsgi (Uwsgi--reload/tmp/uwsgi.pid)
--daemonize, increase the daemon and make the Web service more stable. parameter is the path to the log file.
Other slightly, you can try them one by one.
Use
Flask must be used in combination.
Django recommends using the default support, which has the default wsgi.py file generation.
Uwsgi using Essays