0

I have a django app running on apache. I use mod_wsgi. I'm looking to set a few env variables in the wsgi.py script before creating the 'application'. But I want to set the env variables based on certain properties in the request header.

I can't set the env variables using a django middleware because the env variables are needed at the time of loading some of the python modules which seems to happen before the middleware's process_requestfunction is invoked.

So my question is - How/where can I initialize the env variables based on the request header before the 'application' is created in my wsgi script?

This is how I'm creating the application in wsgi.py -

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

The import of django.core.handlers.wsgi is what is triggering loading of my application's python modules that need the env variables to be defined while loading.

Thanks,

7
  • Please explain the original problem you are trying to solve rather than what your perceived solution is. Knowing the original problem you are having and so why you think you need this would help us to propose alternate solutions for the original problem which would be better. Especially in the context of mod_wsgi I can explain better ways of getting configuration information setup based on things being done in the Apache configuration file. Relying on headers is dangerous for a number of reasons. Commented Sep 18, 2014 at 20:09
  • @GrahamDumpleton, I was actually hoping you would provide your inputs for the question. The original problem in question is this - stackoverflow.com/questions/25054347/… . I realized that it is not possible to change the settings in a reliable way. So I have multiple databases defined in django settings I have monkey patched the connection handler to return a different database connection for 'default' alias based on certain env variables set in the middleware. For this problem, I've made sure DB is not queried while loading modules. Commented Sep 19, 2014 at 7:31
  • How many distinct sites/database configurations are you trying to host within the one Django instance? How much throughput do you get on requests for each. Trying to do things like that in one process isn't generally a good idea. There are perhaps better ways depending on the scale at which you are doing things as far as number of variations. Commented Sep 19, 2014 at 8:21
  • And are you using embedded mode or daemon mode and if embedded prefork or worker MPM. In either case how many processes/threads for either the MPM or daemon mode. As far as memory goes, a big mistake people do is use use prefork MPM. Use daemon mode and you can cut back on memory and afford to have distinct instances no problems. Commented Sep 19, 2014 at 8:28
  • It's a SaaS application. So the django app source is one. But we want to add multiple DBs for each client in the same settings. Maintaining separate settings.py for each client is not allowing use to scale efficiently. At this point there will probably be about 10-15 DBs in one cluster. We will of course scale horizontally by adding more (same config) nodes to the cluster. We are using embedded mode (I think). We do prefork apache processes. I'd like to learn more about the differences between daemon and embedded modes. Could you point me to some documentation that explains the differences? Commented Sep 20, 2014 at 10:01

1 Answer 1

0

You can't and shouldn't. The WSGI process is persistent across multiple requests, you don't initialize it from new each time.

If your middleware depends on certain elements in the request, you should check those explicitly there, rather than relying on environment variables.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I'm aware that the wsgi script is executed once for each process and that the process handles multiple requests. Wouldn't that first initialization also be based on a request (the first one)? I'm able to handle it nicely from the middleware for most cases except when loading of modules before the middleware is called. I didn't design the modules to be that way, but I'm having to deal with it.
I think I'm going to clean up the code so there is no need for the env variables during modules load and instead rely on doing it through the middleware. I will wait for a while to see if there are any other interesting answers and then accept this answer. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.