开发者

Buildout config for multiple Django WSGI files

开发者 https://www.devze.com 2023-02-14 12:25 出处:网络
I\'ve got a Django project that has multiple settings files (www site, mobile site, an API..) and I recently got around to cutting the config/deployment over to buildout. Unfortunately, the only way I

I've got a Django project that has multiple settings files (www site, mobile site, an API..) and I recently got around to cutting the config/deployment over to buildout. Unfortunately, the only way I can get djangorecipe to generate separate WSGI files for me is to specify each site as its own block, which creates a whole separate django lib for each one.

I suppose that's not really a problem per se, and a workaround would be to manually create the WSGI files...but if there was a way to have that all happen by buildout instead and share the same django lib, that would be ideal.

Here's what I have now, which creates separate Django installs:

[buildout]
parts =
    python
    web
    mobile
    <etc...>

[python]
recipe = zc.recipe.egg
eggs = <etc...>

[web]
recipe = djangorecipe
interpreter = python
version = trunk
project = proj
settings = web_s开发者_如何学Pythonettings
eggs = ${python:eggs}
wsgi = true

[mobile]
recipe = djangorecipe
interpreter = python
version = ${web:version}
project = ${web:project}
settings = mobile_settings
eggs = ${python:eggs}
wsgi = true

<etc...>


Since it is not possible to set environment variable in apache config and retrieve it in wsgi script (explained in: mod-wsgi wiki) it seems that the most elegant solution is to have separate wsgi scripts.

(There was a related discussion on SO: Django - Can't pass Environment Variable to Apache/Passenger on the WSGI Interface)

Now if you are about to create wsgi scripts manually you will have to deal with sys.path manually. So it seems that to it is easier to have few sections of django.recipe.


It is also possible not to use django-recipe at all. At least I prefer this because then I have full freedom to setup my wsgi/manage scripts. And it is not that hard to configure buildout in a way that it will wrap your manually written scripts into bin folder with sys.path automatically configured.

Here is how to achieve it:

  • Create your wsgi, manage scripts manually at myproject.scrpits.* as described in django documentation. However, wrap the "active" part to a def main(): method. Then, your scripts can be used as modules.

  • Create proper setup.py script for your project. It will install scripts which you created in the first step. Entry-points part is important here:

    from distutils.core import setup
    setup(name='mygroject',
          packages=['myproject'],
          entry_points="""
          [console_scripts]
          manage = myproject.scripts.manage:main
          wsgi = mygroject.scripts.wsgi:main
          """
         )
    
  • Configure buildout. python:scripts is important here:

    [buildout]
    ...
    # Add directory where your setup.py can be found.
    # I assume that you placed your setup.py in same directory as your buildout file.
    develop=.
    
    [python]
    recipe = zc.recipe.egg:scripts
    # django is no longer instlled by django-recipe so has to be listed in eggs.
    # myproject also has to be listed here.
    eggs= ... django myproject
    # Now scripts from the setup.py:
    scripts = manage wsgi
    # fallowing will create bin/python, might be useful:
    interpreter = python
    
  • Now buildout will generate scripts in bin folder which you defined in steps 1-2.


Every wsgi script that you run behind your webserver is a separate entity. So: is the django settings module that you use the only difference? I mean, are you really sure it is safe to run two or more separate sites in the same directory? Take care of shared directories that should not really be shared and so. The - possibly safer - alternative to what you're doing is to just have two separate buildouts.

On the other hand, you say you've got it working now by using two djangorecipe parts. Well, what's the real problem? OK, django is duplicated. Couple of megabytes: how bad is that? Upload three big photos to your website and that takes the same amount of space. So: isn't your current solution good enough already?

Third comment: I think your setup is pretty much unique, so getting support for that in djangorecipe itself will be hard. You could try and get it working, I think the recipe is on launchpad, ready to fork.

0

精彩评论

暂无评论...
验证码 换一张
取 消