Passing variables to a wsgi script from mod_wsgi apache configuration

922 views
Skip to first unread message

inaf

unread,
Nov 14, 2009, 12:06:54 PM11/14/09
to modwsgi
I am using mod_wsgi 2.4 in daemon mode. I have an object that gets
initialized and created as a singleton object that gets used by the
same .wsgi file. What initializes it with the parameters is the first
call ever made to the .wsgi file. After that point, though they all
initialize the object, they all get the same singleton object. To make
things more manageable, I would like have those parameters in mod_wsgi
apache configuration so that I can read them from there in my .wsgi
file to initialize the singleton object. These parameters are simply
URLs for memcached servers that my .wsgi file connects to. Since I
have multiple environments and each environment will be using
different memcached servers, I would like to have these URLs set as
parameters in apache configuration files rather than in my .wsgi file.
Is there a way to achieve this? I looked through the documentation but
I could not find anything. Any help would be greatly appreciated.

Thanks,
-Cem

Jason Garber

unread,
Nov 14, 2009, 10:34:41 PM11/14/09
to mod...@googlegroups.com
Hi Cem,

Not exactly an "anwser", but we auto-generate a "Local.py" file per installation (as well as our apache configuration files).  Local.py linked to from the main application package directory (you can set the python path from apache configuration).

class App:
   from .Local import MemcachedServers, SecureURL, StandardURL

Then...
   App.SecureURL

-JG

Graham Dumpleton

unread,
Nov 14, 2009, 11:32:58 PM11/14/09
to mod...@googlegroups.com
2009/11/15 Jason Garber <bo...@gahooa.com>:
> Hi Cem,
> Not exactly an "anwser", but we auto-generate a "Local.py" file per
> installation (as well as our apache configuration files).  Local.py linked
> to from the main application package directory (you can set the python path
> from apache configuration).
> class App:
>    from .Local import MemcachedServers, SecureURL, StandardURL
> Then...
>    App.SecureURL

I'd be suggesting something similar, ie., using something that can't
change per request.

That said, when you use SetEnv directive, those variables are
available in the WSGI environ dictionary passed as first argument to
the WSGI application object when called for a request. You could
extract them from the WSGI environ and then use them. Because though
they are per request and only available when application called for a
request, you would need thread locking on the creation of the
singleton.

Option options are to key off value of mod_wsgi.application_group.
This is not the one in WSGI environment though, but from doing:

import mod_wsgi
if mod_wsgi.application_group.find('xxx') != -1:
...

The value of mod_wsgi.application_group by default incorporates site
server name and mount point of WSGI application. If your different
staging environments are on different hosts, then you can distinguish
that way.

One could also use mod_wsgi.process_group. So, if using
WSGIDaemonProcess/WSGIProcessGroup to separate instances, you could
customise behaviour based on name of process group.

import mod_wsgi
if mod_wsgi.process_group == 'development':
...

You need mod_wsgi 2.4 or later to get access to these special module
variables, but you are on 2.4 so okay.

Graham
Reply all
Reply to author
Forward
0 new messages