views:

30

answers:

2

I have a project which has a directory setup like:

myproject
  someapp
  sites
      foo
         settings.py - site specific 
  settings.py - global

I am using twisted web.wsgi to serve this project. The problem am I running into is setting up the correct environment.

import sys
import os
from twisted.application import internet, service
from twisted.web import server, resource, wsgi, static, vhost
from twisted.python import threadpool
from twisted.internet import reactor
from django.core.handlers.wsgi import WSGIHandler
from  django.core.management import setup_environ,ManagementUtility
sys.path.append(os.path.abspath("."))
sys.path.append(os.path.abspath("../"))
DIRNAME= os.path.dirname(__file__)
SITE_OVERLOADS = os.path.join(DIRNAME,'sites')

def import_module(name):
 mod = __import__(name)
 components = name.split('.')
 for comp in components[1:]:
  mod = getattr(mod,comp)
 return mod
def buildServer():
 hosts = [d for d in os.listdir(SITE_OVERLOADS) if not os.path.isfile(d) and d != ".svn"]
 root = vhost.NameVirtualHost()
 pool = threadpool.ThreadPool()
 pool.start()
 reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)

 for host in hosts:
  settings = os.path.join(SITE_OVERLOADS,"%s/settings.py" % host)
  if os.path.exists(settings):
   sm = "myproject.sites.%s.settings" % host
   settings_module = import_module(sm)
   domain = settings_module.DOMAIN
   setup_environ(settings_module)
   utility = ManagementUtility()
   command = utility.fetch_command('runserver')
   command.validate()
   wsgi_resource = wsgi.WSGIResource(reactor,pool,WSGIHandler())
   root.addHost(domain,wsgi_resource)
 return root

root = buildServer()
site = server.Site(root)
application = service.Application('MyProject')
sc = service.IServiceCollection(application)
i = internet.TCPServer(8001, site)
i.setServiceParent(sc)

I am trying to setup vhosts for each site which has a settings module in the subdirectory "sites". However, it appears that the settings are being shared for each site.

+1  A: 

Django projects within the same Python process will share the same settings. You will need to spawn them as separate processes in order for them to use separate settings modules.

Ignacio Vazquez-Abrams
how would you spawn each one of the sites into it's own process?
tomfmason
Unfortunately I'm not yet familiar enough with Twisted to answer that.
Ignacio Vazquez-Abrams
A: 

Since your goal is a bunch of shared-nothing virtual hosts, you probably won't benefit from trying to set up your processes in anything but the simplest way. So, how about changing your .tac file to just launch a server for a single virtual host, starting up a lot of instances (manually, with a shell script, with another simple Python script, etc), and then putting a reverse proxy (nginx, Apache, even another Twisted Web process) in front of all of those processes?

You could do this all with Twisted, and it might even confer some advantages, but for just getting started you would probably rather focus on your site than on minor tweaks to your deployment process. If it becomes a problem that things aren't more integrated, then that would be the time to revisit the issue and try to improve on your solution.

Jean-Paul Calderone