Scoping SQLAlchemy's Session while using Tornado.gen

Note: This article is not about using SQLAlchemy and Tornado to do asynchronous calls to your database. It is about making sure each request uses the correct session even if it does async calls to other services.

Session management with SQLAlchemy and Tornado is a little bit tricky if you are doing asynchronous calls. You have to be careful to make sure your request is using the same session before and after an async call. The recommended way to do this is to associate a session with the request itself, but this is not feasible if you are using declarative_base. I got around this issue by making my own scoping function and patching tornado. 

The scoping function is extremely simple. The get method it defines is used by scoped_session to determine the correct session object to provide from its registry. The handlers and patches will use to the set method to set the correct session scope.

We create a base handler that sets the correct scope using the scope object created in

Finally, we do a little bit of monkey patching so the correct scope will be maintained before and after async calls using tornado.gen

With all this in place, we can safely have declarative base models and commit sessions whenever we want.

A Better Calorie Tracker

I have a love hate relationship with calorie trackers. Over the past few years, I have been using loseIt and myFitnessPal to help me get in better shape. The apps help me make better decisions on which foods to eat to reach my daily calorie, protein, fat, and carbohydrate goals. In fact, without a calorie tracker following my diet plan would be impossible. Even with all of these benefits, I hate using calorie trackers because they make entering food extremely painful and annoying.

Let's take a look at myFitnessPal. When you launch the app you are presented with this:

When I launch a calorie tracker, I am doing so because I either want to add some food or see what I can eat next that will fit within my goals. My daily goals change because I eat more calories and carbs on workout days.

Keeping that in mind, this home screen is almost worthless. Most of it is taken up by a newsfeed with stories that I do not care about. It also has controls to change the date even though that information is useless today. No information about protein, carbs, and fat are shown. These are important because without them it is hard to make a decision on what type of food to eat next.

To get to macros in myFitnessPal, I have to tap some non obvious button and then hit another button. After all that work, I am presented with this view that has too much irrelevant information.

To fix these issues, I would propose a home screen that looks like this (please excuse the shitty wireframe mockups):

I get all the important information that I need at a quick glance. There is no extra cruft and there is a huge button that makes it super easy to input your food.

Next, we go to the actual process of inputting food using myFitnessPal. After tapping add to diary on the home screen you go through the following flow:

Why do I need to input what meal this was a part of? Who cares? The food should just be added to my daily total. After clicking breakfast, I am presented with a search screen. It is not bad, but why do I need to choose different tabs to narrow my search? That all should just be taken care of by the app. Finally, the screen to actually add the food has too much irrelevant data.

Many times when I am on these screens, I am deciding on what I can eat. I might be planning my dinner or deciding what snack would be best. There is no easy way to do any of this with this flow.

The flow would be better this way:

Your current macros and calories would be readily available. This way you can easily see where you currently are in your daily allotment. The search screen would show the standard serving of each food and what its statistics allowing you to make better choices on what to eat. It would display your most recently logged food as well as food you tend to log at the current time on other days. Current totals for each item logged today would show in that item's row.

The add screen is also dramatically simplified. We only show the calories and macro nutrients for each item. To allow you to make better judgements on how much to eat, your new statistics are shown when you change the # of serving or the serving size.

I believe these changes would make a much better user experience. However, one of the biggest competitive advantages calorie tracking apps have right now is the size of their food databases. MyFitnessPal has a huge set of data and users are inputting more data everyday. A better calorie tracker will have to have better UX as well as a robust database of food.