WSGI

#wsgi #web

##What is WSGI

WSGI is the Web Server Gateway Interface. It is a specification for web servers and application servers to communicate with web applications (though it can also be used for more than that). It is a Python standard, described in detail in PEP 333.

WSGI Performance Estimates

Mechanism     Requests/sec
mod_cgi ( ScriptAlias )     10
mod_python ( PythonHandler )     400
mod_wsgi ( WSGIDaemonProcess )     700
mod_wsgi ( .htaccess/SetHandler )     850
mod_wsgi ( WSGIScriptAlias )     900
static ( DocumentRoot )     1000

##Apache

安装

sudo apt-get install apache2

访问

http://localhost/

性能

ab -n1000 -c100 http://localhost/index.html   # 14000

##mod_wsgi (WSGIScriptAlias)

安装

sudo apt-get install libapache2-mod-wsgi

新增文件:/etc/apache2/mods-available/{wsgi.load,wsgi.conf}

配置/etc/apache2/httpd.conf

LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi

<Directory /var/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

创建文件

sudo mkdir -p /var/www/wsgi-scripts
sudo vim /var/www/wsgi-scripts/myapp.wsgi

编辑myapp.wsgi

def application(environ, start_response):
    status = '200 OK'
    output = 'mod_wsgi works!'

    response_headers = [('Content-type', 'text/plain'),('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

变更生效

sudo service apache2 reload

访问

http://localhost/myapp

性能

ab -n1000 -c100 http://localhost/myapp/    # 11000

##mod_wsgi (WSGIDaemonProcess)

By default any WSGI application will run in what is called embedded mode. That is, the application will be hosted within the Apache worker processes used to handle normal static file requests.

In daemon mode a set of processes is created for hosting a WSGI application, with any requests for that WSGI application automatically being routed to those processes for handling.

修改配置/etc/apache2/httpd.conf

LoadModule wsgi_module modules/mod_wsgi.so

WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example.com

WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi

<Directory /var/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

性能

ab -n1000 -c100 http://localhost/myapp/    # 7000 ##文档

https://code.google.com/p/modwsgi/