2

In WSGI file, we would import a py file as

from <pyFile> import app as application

but is it possible to load several py file into a single wsgi file by doing something like this:

from <pyFile1> import app1 as application
from <pyFile2> import app2 as application

I've tried the above, and it doesn't work.

Is there a different way to achieve this?

daedalus
  • 10,873
  • 5
  • 50
  • 71
Noor
  • 19,638
  • 38
  • 136
  • 254
  • of course it does not work, how would you then differentiate between the two modules named `application` ? – Adrien Plisson Jun 03 '12 at 10:23
  • Clarify what WSGI hosting mechanism you are using. Some have ability to have multiple application entry points in same WSGI file, so long as each is named differently, and the WSGI server configuration is setup to map different URLs to them. – Graham Dumpleton Jun 03 '12 at 11:45

2 Answers2

2

If uwsgi is your implementation of choice, consider this:

import uwsgi

from <pyFile1> import app1 as application1
from <pyFile2> import app2 as application2

uwsgi.applications = {'':application1, '/app2':application2}
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

You can't import various modules as the same name, e.g.

from moduleX import somevar as X
from moduleY import othervar as X

Results in X == othervar.

But anyway you can't have multiple applications running in the same instance of Python. That's because

The application object is simply a callable object that accepts two arguments [PEP 333].

Now, a simple WSGI application is something like:

def simple_app(environ, start_response):
    """Simplest possible application object"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

As you can see, there is no place here to make multiple applications working at the same time, due to the fact that every request is passed to ONE particular application callback.

Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83