tags:

views:

99

answers:

6

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?

+1  A: 

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.

Oren Mazor
I'm more comfortable with Nginx+Upstreams so would stick with that. Thanks anyway, but I'm not sure that answers my question. I have clarified further.
digitala
Oh, I see what you meant now.I'm not really sure, as this is a part time hobby for me, but from what I can tell, I would guess that python itself would handle the ordering for you if its a question of dependencies.now, if you're trying to force loading of specific modules in a specific order, I dont know. but its a good question.
Oren Mazor
If it really is a good question, don't forget to vote it up! ;)
digitala
+1  A: 

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.)

bobince
+3  A: 

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.

Michael Dillon
A: 

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.

Jason Baker
A: 

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).

Steven
A: 

"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.

S.Lott
No, not a big number but if I'm going to re-use the stack having it easily customizable would be handy. I think I'm just getting confused having worked with PHP frameworks that set this sort of thing up in config files.
digitala
It's very, very simple in the long run. What you've shown is perfect. It's not too simple. It will work out perfectly. You don't scale to a lot of packages. Each has unique, distinct, responsibilities so there's no mystery.
S.Lott