Factoring out asynchronous code involving tornado.gen.Task
===

I have numerous tornado.web.RequestHandler classes that test for authorized access using id and access key secure cookies. I access mongodb asynchronously with inline callbacks using gen.Task. I am having trouble figuring out a way to factor out the repetitive code because of its asynchronicity. How can I do this?

    class MyHandler(RequestHandler):
        @tornado.web.asynchronous
        @gen.engine
        def get(self):
            id = self.get_secure_cookie('id', None)
            accesskey = self.get_secure_cookie('accesskey', None)
            if not id or not accesskey:
                self.redirect('/a_public_area')
                return
            try:
                # convert to bson id format to access mongodb
                bson.objectid.ObjectId(id)
            except:
                # if not valid object id
                self.redirect('/a_public_area')
                return
            found_id, error = yield gen.Task(asyncmong_client_inst.collection.find_one, 
                {'_id': id, 'accesskey': accesskey}, fields={'_id': 1})
            if error['error']:
                raise HTTPError(500)
                return
            if not found_id[0]:
                self.redirect('/a_public_area')
                return
            
            # real business code follows

I would like to factor the above into a function that yields perhaps an HTTP status code.