Repository: microsoft/qlib
File: qlib/model/trainer.py (Python)

Code before:
        if isinstance(tasks, dict):
            tasks = [tasks]
        if len(tasks) == 0:
            return []
        if train_func is None:
            train_func = self.train_func
        if experiment_name is None:
            experiment_name = self.experiment_name
        task_pool = self.task_pool
        if task_pool is None:
            task_pool = experiment_name
        tm = TaskManager(task_pool=task_pool)
        _id_list = tm.create_task(tasks)  # all tasks will be saved to MongoDB
        run_task(
            train_func,
            task_pool,
            query={"filter": {"$in": tasks}},  # only train these tasks
            experiment_name=experiment_name,
            before_status=before_status,
            after_status=after_status,
            **kwargs,
        )

        # FIXME: reset to waiting automatically
        for _id in _id_list:
            is_prn = False
            while tm.re_query(_id)["status"] == "running":
                if not is_prn:
                    get_module_logger("TrainerRM").warn(
                        f"A task (_id: {_id}) is not being trained by this Trainer. Ignore this message if it is being trained by others."
                    )
                    is_prn = True
                time.sleep(10)

        recs = []
        for _id in _id_list:
            rec = tm.re_query(_id)["res"]
            rec.set_tags(**{self.STATUS_KEY: self.STATUS_BEGIN})
            recs.append(rec)
        return recs

    def end_train(self, recs: list, **kwargs) -> List[Recorder]:
        """
        Set STATUS_END tag to the recorders.

        Args:
            recs (list): a list of trained recorders.

        Returns:
            List[Recorder]: the same list as the param.
        """

Review comment (suggestion):
I think the following logic will be simpler.
```suggestion
            if   tm.re_query(_id)["status"] == TaskManasger.RUNNING:
                  give the warning.
```

Code after:
        Returns:
            List[Recorder]: a list of Recorders
        """
        if isinstance(tasks, dict):
            tasks = [tasks]
        if len(tasks) == 0:
            return []
        if train_func is None:
            train_func = self.train_func
        if experiment_name is None:
            experiment_name = self.experiment_name
        task_pool = self.task_pool
        if task_pool is None:
            task_pool = experiment_name
        tm = TaskManager(task_pool=task_pool)
        _id_list = tm.create_task(tasks)  # all tasks will be saved to MongoDB
        query = {"_id": {"$in": _id_list}}
        run_task(
            train_func,
            task_pool,
            query=query,  # only train these tasks
            experiment_name=experiment_name,
            before_status=before_status,
            after_status=after_status,
            **kwargs,
        )

        tm.wait(query=query)

        recs = []
        for _id in _id_list:
            rec = tm.re_query(_id)["res"]
            rec.set_tags(**{self.STATUS_KEY: self.STATUS_BEGIN})
            rec.set_tags(**{self.TM_ID: _id})
            recs.append(rec)
        return recs

    def end_train(self, recs: list, **kwargs) -> List[Recorder]:
        """
        Set STATUS_END tag to the recorders.

        Args:
            recs (list): a list of trained recorders.

        Returns:
            List[Recorder]: the same list as the param.
        """
        if isinstance(recs, Recorder):
            recs = [recs]
        for rec in recs:
            rec.set_tags(**{self.STATUS_KEY: self.STATUS_END})