code: def test_trial_reuse_log_to_file(
      trainable, ray_start_1_cpu, tmp_path
  ):
      """Check that log outputs from trainables are correctly stored with actor reuse.
  
      We run two trials with actor reuse. When the actor is reused, we expect
      the log output to be written to the log file of the new trial - i.e. we expect
      that the old trial logfile is closed and a new one is open.
      """
      register_trainable("foo2", trainable)
  
      messages = ["First", "Second"]
      # Log to default files
      [trial1, trial2] = tune.run(
          "foo2",
          config={
              "id": tune.grid_search(list(range(2))),
              "message": tune.sample_from(
                  lambda config: messages[config["id"]]
              ),
              "marker_dir": tmp_path,
          },
          log_to_file=True,
          scheduler=FrequentPausesScheduler(),
          reuse_actors=True,
      ).trials
  
      # Check trial 1
      assert trial1.last_result["num_resets"] == 2
      assert os.path.exists(os.path.join(trial1.local_path, "stdout"))
      assert os.path.exists(os.path.join(trial1.local_path, "stderr"))
  
      # We expect that only "First" output is found in the first trial output
      with open(os.path.join(trial1.local_path, "stdout"), "rt") as fp:
          content = fp.read()
          assert "PRINT_STDOUT: First" in content
          assert "PRINT_STDOUT: Second" not in content
      with open(os.path.join(trial1.local_path, "stderr"), "rt") as fp:
          content = fp.read()
          assert "PRINT_STDERR: First" in content
          assert "LOG_STDERR: First" in content
          assert "PRINT_STDERR: Second" not in content
          assert "LOG_STDERR: Second" not in content
  
      # Check trial 2
      assert trial2.last_result["num_resets"] == 3
      assert os.path.exists(os.path.join(trial2.local_path, "stdout"))
      assert os.path.exists(os.path.join(trial2.local_path, "stderr"))
  
      # We expect that only "Second" output is found in the first trial output
      with open(os.path.join(trial2.local_path, "stdout"), "rt") as fp:
          content = fp.read()
          assert "PRINT_STDOUT: Second" in content
          assert "PRINT_STDOUT: First" not in content
      with open(os.path.join(trial2.local_path, "stderr"), "rt") as fp:
          content = fp.read()
          assert "PRINT_STDERR: Second" in content
          assert "LOG_STDERR: Second" in content
          assert "PRINT_STDERR: First" not in content
          assert "LOG_STDERR: First" not in content
  
imports: import inspect
  import os
  from pathlib import Path
  import tempfile
  from typing import Callable
  import pytest
  import sys
  import time
  import ray
  from ray import train, tune, logger
  from ray.train import CheckpointConfig
  from ray.tune import Trainable, run_experiments, register_trainable
  from ray.tune.error import TuneError
  from ray.tune.result_grid import ResultGrid
  from ray.tune.schedulers.trial_scheduler import FIFOScheduler, TrialScheduler
  from ray.tune.tune import _check_mixin
  from ray.train.tests.util import create_dict_checkpoint, load_dict_checkpoint
label_desc: Use image node_id 12 for calling a global function with example usage: test_trial_reuse_log_to_file(trainable, ray_start_1_cpu, tmp_path) without return types