def run_top_task(self, task_name=None, sort=None, **kwargs):
if not isinstance(task_name, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:status>': '<STR_LIT>'})<EOL>task = self.db.Task.find_one_and_update(kwargs, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}}, sort=sort)<EOL>try:<EOL><INDENT>if task is None:<EOL><INDENT>logging.info("<STR_LIT>".format(task_name, sort))<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(task_name, sort))<EOL><DEDENT>_datetime = task['<STR_LIT:time>']<EOL>_script = task['<STR_LIT>']<EOL>_id = task['<STR_LIT>']<EOL>_hyper_parameters = task['<STR_LIT>']<EOL>_saved_result_keys = task['<STR_LIT>']<EOL>logging.info("<STR_LIT>")<EOL>for key in _hyper_parameters:<EOL><INDENT>globals()[key] = _hyper_parameters[key]<EOL>logging.info("<STR_LIT>".format(key, _hyper_parameters[key]))<EOL><DEDENT>s = time.time()<EOL>logging.info("<STR_LIT>".format(task_name, sort, _datetime))<EOL>_script = _script.decode('<STR_LIT:utf-8>')<EOL>with tf.Graph().as_default():  <EOL><INDENT>exec(_script, globals())<EOL><DEDENT>_ = self.db.Task.find_one_and_update({'<STR_LIT>': _id}, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}})<EOL>__result = {}<EOL>for _key in _saved_result_keys:<EOL><INDENT>logging.info("<STR_LIT>".format(_key, globals()[_key], type(globals()[_key])))<EOL>__result.update({"<STR_LIT:%s>" % _key: globals()[_key]})<EOL><DEDENT>_ = self.db.Task.find_one_and_update(<EOL>{<EOL>'<STR_LIT>': _id<EOL>}, {'<STR_LIT>': {<EOL>'<STR_LIT:result>': __result<EOL>}}, return_document=pymongo.ReturnDocument.AFTER<EOL>)<EOL>logging.info(<EOL>"<STR_LIT>".<EOL>format(task_name, sort, _datetime,<EOL>time.time() - s)<EOL>)<EOL>return True<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>logging.info("<STR_LIT>")<EOL>_ = self.db.Task.find_one_and_update({'<STR_LIT>': _id}, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}})<EOL>return False<EOL><DEDENT>
Finds and runs a pending task that in the first of the sorting list.

        Parameters
        -----------
        task_name : str
            The task name.
        sort : List of tuple
            PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details.
        kwargs : other parameters
            Users customized parameters such as description, version number.

        Examples
        ---------
        Monitors the database and pull tasks to run
        >>> while True:
        >>>     print("waiting task from distributor")
        >>>     db.run_top_task(task_name='mnist', sort=[("time", -1)])
        >>>     time.sleep(1)

        Returns
        --------
        boolean : True for success, False for fail.