(
        self, study_id: int, template_trial: Optional[FrozenTrial] = None
    )
Create a new trial and returns its trial_id and a :class:`~optuna.trial.FrozenTrial`.

        Args:
            study_id:
                Study id.
            template_trial:
                A :class:`~optuna.trial.FrozenTrial` with default values for trial attributes.

        Returns:
            A :class:`~optuna.trial.FrozenTrial` instance.
Create a new trial and returns its trial_id and a :class:`~optuna.trial.FrozenTrial`.
Create a new trial and returns its trial_id and a : class : ~optuna . trial . FrozenTrial .
def _create_new_trial(
        self, study_id: int, template_trial: Optional[FrozenTrial] = None
    ) -> FrozenTrial:
        """Create a new trial and returns its trial_id and a :class:`~optuna.trial.FrozenTrial`.

        Args:
            study_id:
                Study id.
            template_trial:
                A :class:`~optuna.trial.FrozenTrial` with default values for trial attributes.

        Returns:
            A :class:`~optuna.trial.FrozenTrial` instance.

        """

        # Retry a couple of times. Deadlocks may occur in distributed environments.
        n_retries = 0
        with _create_scoped_session(self.scoped_session) as session:
            while True:
                try:
                    # Ensure that that study exists.
                    #
                    # Locking within a study is necessary since the creation of a trial is not an
                    # atomic operation. More precisely, the trial number computed in
                    # `_get_prepared_new_trial` is prone to race conditions without this lock.
                    models.StudyModel.find_or_raise_by_id(study_id, session, for_update=True)

                    trial = self._get_prepared_new_trial(study_id, template_trial, session)
                    break  # Successfully created trial.
                except OperationalError:
                    if n_retries > 2:
                        raise

            n_retries += 1

            if template_trial:
                frozen = copy.deepcopy(template_trial)
                frozen.number = trial.number
                frozen.datetime_start = trial.datetime_start
                frozen._trial_id = trial.trial_id
            else:
                frozen = FrozenTrial(
                    number=trial.number,
                    state=trial.state,
                    value=None,
                    values=None,
                    datetime_start=trial.datetime_start,
                    datetime_complete=None,
                    params={},
                    distributions={},
                    user_attrs={},
                    system_attrs={},
                    intermediate_values={},
                    trial_id=trial.trial_id,
                )

            return frozen
def _create_new_trial ( self , study_id : int , template_trial : Optional [ FrozenTrial ] = None ) -> FrozenTrial : # Retry a couple of times. Deadlocks may occur in distributed environments. n_retries = 0 with _create_scoped_session ( self . scoped_session ) as session : while True : try : # Ensure that that study exists. # # Locking within a study is necessary since the creation of a trial is not an # atomic operation. More precisely, the trial number computed in # `_get_prepared_new_trial` is prone to race conditions without this lock. models . StudyModel . find_or_raise_by_id ( study_id , session , for_update = True ) trial = self . _get_prepared_new_trial ( study_id , template_trial , session ) break # Successfully created trial. except OperationalError : if n_retries > 2 : raise n_retries += 1 if template_trial : frozen = copy . deepcopy ( template_trial ) frozen . number = trial . number frozen . datetime_start = trial . datetime_start frozen . _trial_id = trial . trial_id else : frozen = FrozenTrial ( number = trial . number , state = trial . state , value = None , values = None , datetime_start = trial . datetime_start , datetime_complete = None , params = { } , distributions = { } , user_attrs = { } , system_attrs = { } , intermediate_values = { } , trial_id = trial . trial_id , ) return frozen