Question

After looking through the many useful and shiny Python frameworks, I find none of them get close to what I need or provide way more than my needs. I'm looking to put something together myself; could define it as a framework, but not full-stack. However, I can't find online what the Python community sees as the correct/standard way to manage WSGI middleware in an application.

I'm not looking for framework suggestions, unless its to provide an example of ways to manage WSGI middleware. Nor am I looking for information on how to get a webserver to talk to python -- that bit I understand.

Rather, I'm looking for advice on how one tells python what components/middleware to put into the stack, and in which order. For instance, if I wanted to use:
Spawning-->memento-->AuthKit-->(?)-->MyApp
how would I get those components into the right order, and how would I configure an additional item (say Routes) before MyApp?

So; Can you advise on the common/correct/standard way of managing what middleware is included in a WSGI stack for a Python application?

Edit
Thanks to Michael Dillon for recommending A Do-It-Yourself Framework, which helps highlight my problem. The middleware section of that document states that one should wrap middleware A in middleware B, B in C, and so-on:

app = ObjectPublisher(Root())
wrapped_app = AuthMiddleware(app)
from paste.evalexception import EvalException
exc_wrapped_app = EvalException(wrapped_app)

Which shows how to do it in a very simple way. I understand how this works, however it seems too simple when working with a number of middleware packages.

Is there a better way to manage how these middleware components are added to the stack? Maybe a common design pattern which reads from a config file?

Was it helpful?

Solution

That is what a framework does. Some frameworks like Django are fairly rigid and others like Pylons make it easier to mix and match.

Since you will likely be using some of the WSGI components from the Paste project sooner or later, you might as well read this article from the Paste folks about a Do-It-Yourself Framework. I'm not suggesting that you should go and build your own framework, but that the article gives a good explanation of how the WSGI stack works and how things go together.

OTHER TIPS

I'd have to say that Apache/mod_wsgi is probably the most "manageable" of the setups I've used.

nginx/fcgi is the fastest, but its a bit of a headache.

What middleware do you think you need? You may very well not need to include any WSGI ‘middleware’-like components at all. You can perfectly well put together a loose ‘pseudo-framework’ of standalone libraries without needing to ‘wrap’ the application in middleware at all.

(Personally I use a separate form-reading library, data access layer and template engine, none of which know about each other or need to start fiddling with the WSGI environ.)

My advice is to read the PEP on WSGI, specifically the part on middleware. If you have a question about anything with the words "standard" and "WSGI" in it, the answer is either there, or you're asking the wrong question.

If you liked the Do-It-Yourself-Framework tutorial mentioned before, but you want to manage these things in a config file, Paste Deploy would be the obvious answer. (It is mentioned in the tutorial, but only very briefly in the very last paragraph).

This is what the Pylons framework uses, by the way (and Turbogears 2, which is built upon Pylons, also).

"it seems too simple when working with a number of middleware packages."

How big a number?

You won't be working with hundreds or thousands.

It will be (a) a small number (under a dozen) and (b) the "right" order isn't magical.

Each piece of middleware will have a very, very specific job and very specific requirements for what must come before it.

It's much less confusing than you're assuming.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top