This logic would probably be much cleaner if we use `tornado.gen`, but I will leave that up to you.