[Flask] Subclassing flask.Flask?

Skip Montanaro skip.montanaro at gmail.com
Fri Mar 31 08:48:46 EDT 2017

I'm getting to the point with a smallish Flask application that I
really don't want a bunch of module-level global variables sitting
around. My initial inclination would be to subclass flask.Flask, but
picking through the documentation and the prominent examples
(minitwit, etc), I didn't see any examples of this. This leads me to
believe that maybe that's not the correct route for corralling my

I'll give you one small example. My database is actually constructed
from a set of files which are updated outside of my control. I'd like
to run a separate thread to incrementally update the relevant bits of
my database as individual files change. The straightforward (to me)
way to do this is to have a separate thread which keeps things
up-to-date. As I'm currently using sqlite3, that suggests I need to
lock access. Storing the necessary threading.{RLock,Lock} object at
the module level seems iffy. I'd prefer to keep it as an instance
attribute. Once I've got a subclass, why not put all the
implementation in instance methods with the necessary @app.route

So, am I off-base in thinking I should subclass the Flask class? Is
there a more prevalent pattern?


Skip Montanaro

More information about the Flask mailing list