IMServer:mod wsgi
wsgi interface in Apache
- Recompile with the matching python version
- I had issues with python 2.4.* but worked well with python 2.6.*
When wsgi interface uses modules from the local directory, the script should first append python path as follows otherwise the script which runs without errors in debug mode will not be able to run when called from wsgi interface.
<source lang="python">
sys.path.append('/var/www-dj/') from mongo import m
</source>
wsgi in windows
- Download and install prebuilt Apache (32 bit msi)
- prebuilt
- Edit the httpd.conf configuration file and add
LoadModule wsgi_module modules/mod_wsgi.so
- Modify directory configuration to execute scripts
WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/myapp.wsgi
- Add approximately following section to setup virtualhost responding to wsgi
# Ensure that Apache listens on port 80 Listen 82 # Listen for virtual host requests on all IP addresses NameVirtualHost *:82 <VirtualHost *:82> DocumentRoot "C:/Dhan/git/imserver-python" Options Indexes FollowSymLinks ServerName localhost AddHandler wsgi-script .py WSGIScriptAlias / c:/dhan/git/imserver-python/ <Directory c:/dhan/git/imserver-python> Options FollowSymLinks AllowOverride all Order allow,deny Allow from all </Directory> </VirtualHost>
Development
- Any wsgi script must be able to maintain persistent connection with mongo to boost speed
Testing persistent interpreter
sys.path.append("/var/www-dj/")
try:
import test6
except:
class sm():
def __init__(self):
pass
self.x = 177;
test6 = sm()
def application(environ, start_response):
""" The WSGI test application """
# emit status / headers
status = "200 OK"
str1 = "New value of X is %d"%(test6.x)
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(str1)))]
start_response(status, response_headers)
test6.x = test6.x+1
return [str1]
</source>
The test6.py in the python path contains a single line
<pre>
x = 199
This takes advantage of the singleton pattern of python modules.
If test6.py is newly loaded in the run time the value of x will be initialized to 199 every time. And hence the output will always be 200. If the module fails to load (due to incorrect path etc.) then the value will be 178. And if the module is just loaded once due to persistent interpreter for multiple calls, the value will be initialized to 199 during loading, and will continuously increase in subsequent calls.