As example, consider the simple bobo application as follows:
import bobo
@bobo.query
def hello():
return "Hello world!"
All one has to do to turn this into a WSGI script file suitable for use with mod_wsgi is add the single line:
application = bobo.Application(bobo_resources=__name__)
This one line creates an instance of the bobo WSGI application object and assigns it to 'application', the name of the WSGI application entry point which mod_wsgi expects to exist.
The '__name__' variable here is the name of the Python module containing the code. In other words, you are telling bobo to map requests back onto the same code file.
For normal Python modules the '__name__' variable, which is automatically put there by Python, would be the module name and or package path used when importing the module. For mod_wsgi the variable doesn't have quite the same meaning, but still exists, and is still used as the key into 'sys.modules' where the loaded module is placed.
To map the bobo application file at a specific URL, one uses WSGIScriptAlias or AddHandler mechanisms as normal for mod_wsgi.
In mod_wsgi 3.0 there is also a new feature that one could use to define a handler script for all bobo application script files and which transparently performs the job of creating the instance of 'bobo.Application()' for you. If this were used, one could place bobo application script files in a directory using a specific extension, just like with PHP, and not even have to worry about adding the line above. Any change to those files would also result in the process being reloaded if using mod_wsgi daemon mode. I'll talk about this trick another time.
No comments:
Post a Comment