Deploying a Flask Web App as a Dynamic uWSGI App with Multiple Threads

I recently had to deploy a Flask web app for Hush Little Baby Early Childhood Music Classes (shameless plug) with uWSGI. (Sidenote: Flask + SQLAlchemy + WTForms = awesome.) I ran into an extremely exasperating issue which I thought I'd document here in case anyone else runs into it.

Despite the fact that uWSGI recommends that you run a separate instance for each app, I prefer the dynamic app approach. While i certainly understand why separate instances are recommended, I think per-app instances waste resources, especially when they have a lot of common dependencies, including Python itself. I also set uWSGI to use multiple threads. Unfortunately, with Flask, this is a recipe for disaster.

Read more

Getting a Python Traceback without an Exception

Any Python programmer will be familiar with the stack traceback associated with an exception; most will be particularly familiar with a traceback being displayed for an unhandled exception. This is extremely useful in finding and resolving the issue in the code. However, for a long time now, I've often thought that it would be extremely useful to be able to get a stack traceback at any arbitrary point without needing an exception, which would aid in determining what code path was taken to get to a particular function call. I knew this was possible - Python does allow you to get at the stack frames - but I thought I might have to do a bit of work to obtain a nice traceback. Finally, figuring someone else must have wanted this, I did a bit of Googling and turned up this article. I somehow missed print_stack(), format_stack() and extract_stack() in the traceback module. :) Sooo easy! :)