code: def _run_trial(
      study: "optuna.Study",
      func: "optuna.study.study.ObjectiveFuncType",
      catch: Tuple[Type[Exception], ...],
  ) -> trial_module.FrozenTrial:
      if is_heartbeat_enabled(study._storage):
          optuna.storages.fail_stale_trials(study)
  
      trial = study.ask()
  
      state: Optional[TrialState] = None
      value_or_values: Optional[Union[float, Sequence[float]]] = None
      func_err: Optional[Union[Exception, KeyboardInterrupt]] = None
      func_err_fail_exc_info: Optional[Any] = None
  
      with get_heartbeat_thread(trial._trial_id, study._storage):
          try:
              value_or_values = func(trial)
          except exceptions.TrialPruned as e:
              # TODO(mamu): Handle multi-objective cases.
              state = TrialState.PRUNED
              func_err = e
          except (Exception, KeyboardInterrupt) as e:
              state = TrialState.FAIL
              func_err = e
              func_err_fail_exc_info = sys.exc_info()
  
      # `_tell_with_warning` may raise during trial post-processing.
      try:
          frozen_trial = _tell_with_warning(
              study=study,
              trial=trial,
              value_or_values=value_or_values,
              state=state,
              suppress_warning=True,
          )
      except Exception:
          frozen_trial = study._storage.get_trial(trial._trial_id)
          raise
      finally:
          if frozen_trial.state == TrialState.COMPLETE:
              study._log_completed_trial(frozen_trial)
          elif frozen_trial.state == TrialState.PRUNED:
              _logger.info(
                  "Trial {} pruned. {}".format(
                      frozen_trial.number, str(func_err)
                  )
              )
          elif frozen_trial.state == TrialState.FAIL:
              if func_err is not None:
                  _log_failed_trial(
                      frozen_trial,
                      repr(func_err),
                      exc_info=func_err_fail_exc_info,
                      value_or_values=value_or_values,
                  )
              elif STUDY_TELL_WARNING_KEY in frozen_trial.system_attrs:
                  _log_failed_trial(
                      frozen_trial,
                      frozen_trial.system_attrs[STUDY_TELL_WARNING_KEY],
                      value_or_values=value_or_values,
                  )
              else:
                  assert False, "Should not reach."
          else:
              assert False, "Should not reach."
  
      if (
          frozen_trial.state == TrialState.FAIL
          and func_err is not None
          and not isinstance(func_err, catch)
      ):
          raise func_err
      return frozen_trial
  
imports: from concurrent.futures import FIRST_COMPLETED
  from concurrent.futures import Future
  from concurrent.futures import ThreadPoolExecutor
  from concurrent.futures import wait
  import datetime
  import gc
  import itertools
  import os
  import sys
  from typing import Any
  from typing import Callable
  from typing import List
  from typing import Optional
  from typing import Sequence
  from typing import Set
  from typing import Tuple
  from typing import Type
  from typing import Union
  import warnings
  import optuna
  from optuna import exceptions
  from optuna import logging
  from optuna import progress_bar
  from optuna import trial
  from optuna.storages._heartbeat import get_heartbeat_thread
  from optuna.storages._heartbeat import is_heartbeat_enabled
  from optuna.study._tell import _tell_with_warning
  from optuna.study._tell import STUDY_TELL_WARNING_KEY
  from optuna.trial import FrozenTrial
  from optuna.trial import TrialState
label_desc: Use image node_id 3 for calling a global function with example usage: _run_trial(study, func, catch) and returns: frozen_trial