So for my current project ChatFor.Us I’m using asyncmongo with tornado. I’ve started messing around with tornado.gen to try and get rid of the spaghetti callbacks I was building up. First of all, I love tornado.gen, it’s great stuff.
asyncmongo is a special case, in the it doesn’t return just one item to callbacks. Rather, it returns {response, error=None}, or error can be an object if there is an actual error. This gets a little tricky. Turns out I wasn’t the first person to run into this.
https://github.com/facebook/tornado/issues/351
As you can see, the issue has been resolved, but no one really explained how it works anywhere. What Ben did is basically build an Arguments object which is what gets returned. You can access it various ways, and here’s how I’ve found is the simplest for me.
I guess at this point code explains it best.
# execute the query, expecting args and kwargs which is a tuple, dict response
response, error = yield tornado.gen.Task(db.users.find_one, {"id", id})
# result is actually the first item in response which is a tuple
result = response[0]
I have an idea for a modification to make this a bit simpler, and will try to put together a pull request soon.