IMServer:mod wsgi: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Created page with "== 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 ...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[category:Connectome]]
== wsgi interface in Apache ==
== wsgi interface in Apache ==


Line 12: Line 14:


</source>
</source>
== wsgi in windows ==
* Download and install prebuilt Apache (32 bit msi)
* prebuilt 
* Edit the httpd.conf configuration file and add
<pre>
LoadModule wsgi_module modules/mod_wsgi.so
</pre>
* Modify directory configuration to execute scripts
<pre>
WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/myapp.wsgi
</pre>
* Add approximately following section to setup virtualhost responding to wsgi
<pre>
# 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>
</pre>


== Development ==  
== Development ==  


* Any wsgi script must be able to maintain persistent connection with mongo to boost speed
* Any wsgi script must be able to maintain persistent connection with mongo to boost speed
* Script to test persistent connections.
 
== Testing persistent interpreter ==
 
<pre lang="python">
 
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
</pre>
 
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.

Latest revision as of 23:49, 20 February 2011


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.