|
IntegrationWithPylons
How to use Pylons with mod_wsgi.
Integration With PylonsNote: This is not intended as a basic tutorial on how to setup mod_wsgi. It is recommended you first read more introductory material for mod_wsgi. Start by reading through various documents linked off Installation Instructions. The Pylons framework provides the paste.deploy.loadapp() function for constructing a WSGI application stack based on a specific configuration file. Using this function, a script file for a Pylons application which is compatible with mod_wsgi would be constructed as follows: import os, sys
sys.path.append('/usr/local/pylons/mysite')
os.environ['PYTHON_EGG_CACHE'] = '/usr/local/pylons/python-eggs'
from paste.deploy import loadapp
application = loadapp('config:/usr/local/pylons/mysite/development.ini')The directory added to sys.path would be the directory created by running: paster create --template=pylons mysite Because an application is run as the user that Apache runs as, if Python eggs are being used and there is a need for eggs to be unpacked on the fly, it may not have the ability to create a cache directory in the default location. Therefore, it may be necessary to set the environment variable PYTHON_EGG_CACHE at the start of the script file to an appropriate directory where the Apache user has write permission and into which it can unpack egg files. One example of how Apache could be configured would be: WSGIScriptAlias /mysite /usr/local/pylons/mysite/apache/mysite.wsgi <Directory /usr/local/pylons/mysite/apache> Order deny,allow Allow from all </Directory> The configuration shown presumes that an 'apache' subdirectory has been created within the Pylons site instance and the script file stored there under the name 'mysite.wsgi'. Note that you may not be able to use the 'Interpreter' option for the WSGIReloadMechanism directive when using Pylons. This is because some versions of Pylons make use of the PyProtocols package and it is not able to cope with sub interpreters being destroyed and a new sub interpreter being created, from which the PyProtocols package is once again being used. Although newer versions of Pylons may not make direct use of the PyProtocols package, you can still encounter problems because of PyProtocols being used indirectly when packages such as RuleDispatch are present and that is loaded directly by your application or indirectly via packages such a TurboJson. Even the Pylons 'helloworld' example triggers loading of JSON code and so if TurboJson is present it will fail to work with 'Interpreter' reloading. The large level of interdepencies between packages when using Pylons in a large application may make it quite hard to avoid the problem. The result of attempting to use the 'Interpreter' option will be unpredictable, but can range from the Apache child process crashing to random Python exceptions. The actual problems seem to stem from the use of Pyrex generated C code in PyProtocols. At this stage it is not known whether the problems are with how PyProtocols uses Pyrex, or whether Pyrex itself doesn't generate code which is safe to use with multiple sub interpreters. For further suggestions on how to configure mod_wsgi specifically for Pylons, check out the following pages: http://docs.pythonweb.org/pages/viewpage.action?pageId=5439610 |
I got following error when I followed these instructions.
"Nov 14 20:14:45 2007? error? 192.168.0.10? AssertionError?: The EvalException? middleware is not usable in a multi-process environment"
Seems to be that by default apache2 (in my Gutsy gibbon) is multithreaded, and because of that you also have to set "debug = false" property in development.ini file. Possibly full_stack = false would also be sufficient to get rid of this error.
Such constraints on use of EvalException are described in Debugging Techniques document on this wiki.
For solution on how to get Pylons logging to be output, for the moment see:
Normally 'paster' program sets up logging when it is being used to serve application. This initialisation step needs to be simulated in mod_wsgi.
Someone has created a page on Pylons site about the logging setup.
I will update this page eventually, honest. :-)
Config for Pylons + DaemonProcess? + virtualenv (see VirtualEnvironments for details):
Then, your application script doesn't need to redefine PYTHON_EGG_CACHE:
## mysite.wsgi from paste.deploy import loadapp import os config = os.path.join(os.path.dirname(__file__), 'development.ini') application = loadapp('config:%s' % config)Remember to install your application inside virtualenv: