Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Advanced Flask Patterns (speakerdeck.com)
115 points by kracekumar on July 6, 2012 | hide | past | favorite | 26 comments


I love Flask for simple stuff, but I'm a little concerned that, as someone who considers himself a relatively advanced Python developer, I don't really understand quite a lot of this.

I have no doubt that if I dug into it then it would all make sense, but my first instinct is this: how much of this complexity (proxy objects to thread locals, context stacks etc) results from the fact that doing:

    from flask import request
is just a bad idea?

Perhaps things would be easier if instead of:

    from flask import request

    ...

    @app.route('/'):
    def index():
        return "Hello from %s" % request.args.get('name')
we did:

    @app.route('/'):
    def index(request):
        return "Hello from %s" % request.args.get('name')
I'm probably massively over-simplifying things, but the idea of magically importing state is the one aspect of Flask that always felt a bit odd to me.


I was a huge opponent of thread locals for a really long time but I finally had to acknowledge that they make things easier instead of harder. And the reason for this is that thread locals exposed are a very explicit thing compared to magic thread locals hidden somewhere in a framework.

The fact that you can do `_app_ctx_stack.top.mydatabase_connection` to get a database connection from anywhere is very helpful. Frameworks that did not expose thread locals still have them. Just special cased for certain things.

How does User.objects.all() get to the current transaction? Correct: a thread local. It's just hidden in the core of the framework. Not having a thread local there is nearly impossible, especially if you need to work with foreign libraries that do not support passing in of information. It's just going to cause a horrible API. (Not for the view case, but for things like database connections)

Flask does not hide things from you. It is very explicit, honest and upfront with everything it's doing. Is it the best solution? I am pretty sure it's not. I do however think that it's a very viable one.

The talk is very advanced and I think I did not make that clear enough from the summary on the website and I learned from that. I also plan on doing a “postmortem” that gives more context into some of the things the talk was mentioning and why it works the way it works.

TL;DR: you can't live without thread locals or you have a horrible API. Flask embraces them on every level and tells you how you can deal with the downsides of it.


I find this to be true in general: Modern software engineering practices are strongly astronauted, and so well ingrained/indoctrinated, that people think you might be crazy for suggesting globals (or thread locals, which are an evolution of globals) are a good idea.

flask seems to make good use of these. For GUI client programming, FLTK does the same thing marvelously - FLTK code is often 3-4 times shorter and simpler than any other GUI API/framework I've used, and it usually performs much better as well.

In general, the "missing piece" that makes globals/TLS at-least-as-usable-as-pass-everything-explicitly, is "push state"/"pop state", which allows you to do reentrant calls -- usually works much, much better than passing the state as an argument everywhere.

edit: I originally wrote "but flask probably doesn't need a state stack", but then I got to the slide that says it does have them :) silly me, apologies.


Thread locals are addressed in the docs:

    Yes it is usually not such a bright idea to use thread locals. They cause
    troubles for servers that are not based on the concept of threads and make
    large applications harder to maintain. However Flask is just not designed
    for large applications or asynchronous servers. Flask wants to make it quick
    and easy to write a traditional web application.

http://flask.pocoo.org/docs/design/#thread-locals


I haven't used Flask for a large application, but I'm curious why it would be bad for a large application - are there not enough features built in?

What would Python developers prefer to use for a large application - Django?



I feel sad to read remarks like this one :)

If you have a large application, should you use what, Spring? If you use Spring, you'll have a large application.


It should be noted that thread locals play well with greenlets, so it works well with gevent-style asynchronous servers.


We have builtin support for greenlet concurrency even without monkey patching I should add :-)


I want to thank the flask team for this. we are using flask + gevent in production and it has been highly performant (1200 requests a second for our app) and stable :-)


Are you running that on a single server? Care to share the specs?


I do not think the usage of thread locals in Flask is a bad idea. Conceptually request-local variables are no different to stack variables in multiple threads of execution, or member variables of objects in OO languages: both change their value depending on some (often) implicit context. We tolerate both, because we've grown accustomed to them.

Server-side web application frameworks are a well-understood and rigid domain: there is only so much variation you can get in the core structure, so it's easy to keep in mind the meaning and scope of Flask's "magic" implicit state variables.

I have used (and continue using) Flask for small- and medium-size projects, and thread-locals so far haven't been a maintenance burden.


If you're interested in this talk, Armin provides the slides and links to video for many of his previous presentations at his website[1]. The talks range from horrific (but useful) Python hackery to an overview of Python web app development.

[1]: http://lucumr.pocoo.org/talks/


Thank you so much for these. His Flask talks are very intriguing and a good yardstick for measuring your python-fu. The overview of pastebin was enlightening.


This presentation, but with notes by Armin:

http://dev.pocoo.org/~mitsuhiko/FlaskPatterns_notes.pdf


The video is also on YouTube http://www.youtube.com/watch?v=KOvgfbBFZxk&feature=youtu...

Would definitely recommend… I was at the talk yesterday, very clever stuff.


This is what I want. I can never get the whole story from just the slide deck. Need the talking too!


As someone who hasn't used Flask before, this makes me not want to try. I'm not even sure what problem is being solved in this presentation.

Anyone care to explain what these advanced patterns are useful for?


This is basically an explanation of flask internal organization for flask hackers and extension developers. You do you not need such knowledge to create flask applications. Actually due to this features it's possible to have nice and clean end-user programming experience without too much magic. Comparable level of complexity exists in any modern web framework -- just read the source of Django or Rails.


> Anyone care to explain what these advanced patterns are useful for?

It's not intended to be used by newcomers. It's for people that write Flask extensions that should work in a wide range of environments. The word “advanced” is in there for a good reason.


Ahh that makes sense. Maybe it would have been better titled "Advanced patterns for Flask extension developers".


It was indeed labelled as an "advanced talk" at EuroPython (I was at it yesterday).


I haven't had a chance to really read it yet but it looks like it describes thread locals well.

That would make it a lot easier to maintain thread state, e.g. an open database connection, without having to use a singleton.


The video for this would be nice, it's a bit hard understanding some of the slides without context.


Very disappointed this wasn't a gallery of Thermos tartans.


Always happy to see Flask related stuff on HN front page :). Being a novice python wannabe, I am still struggling with thread locals but more than happy to keep working with Flask. Reading the source code is fun.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: