code: def trial_runner_loop(
      channel: str | Path,
      out: str | Path,
      rank: int,
      interval: float,
      patience: float,
      log_buffer_size: int,
  ) -> None:
      output_dir = Path(out)
      runner_dir = output_dir / f"trial_runner_{rank}"
      trial_log_dir = output_dir / f"logs_{rank}"
      runner_dir.mkdir(exist_ok=True, parents=True)
  
      # Init logger if not inited.
      add_handler(_logger, runner_dir / f"trial_runner.log")
  
      _logger.info("Trial runner started.")
  
      _logger.info("Saving trial runner states to: %s", runner_dir)
  
      file_channel = FileChannel(channel, f"worker-{rank}", "manager")
      _logger.info(
          "Using channel %s to communicate with NNI manager.",
          file_channel,
      )
  
      log_buffer_size = log_buffer_size
      _logger.info("Buffer size for trial stdout: %d", log_buffer_size)
  
      trial_runner = TrialRunner(
          file_channel,
          runner_dir,
          output_dir,
          trial_log_dir,
          log_buffer_size,
      )
  
      last_good = time.time()
      last_heartbeat = time.time()
      heartbeat_interval = interval
  
      trial_runner.send_heartbeat()
  
      while True:
          if trial_runner.check_status():
              _logger.info(
                  "Trial runner has running trials. Be patient."
              )
              last_good = time.time()
  
          trial_runner.save_checkpoint()
  
          # Receive a control command from manager side.
          command = file_channel.receive(non_blocking=True)
  
          if command is not None:
              try:
                  command = json.loads(command)
              except:
                  _logger.exception(
                      "Command decode error. Skip this command: %s",
                      command,
                  )
                  command = None
  
          if command is not None:
              if (
                  not isinstance(command, dict)
                  or "command_type" not in command
              ):
                  _logger.error("Invalid command: %s", command)
              else:
                  command_type = command["command_type"]
                  if (
                      command_type == "create"
                      and typed_dict_validation(CreateCommand, command)
                  ):
                      trial_runner.create_trial(command["trial"])
                  elif command_type == "kill" and typed_dict_validation(
                      KillCommand, command
                  ):
                      trial_runner.kill_trial(command["id"])
                  elif (
                      command_type == "wakeup"
                      and typed_dict_validation(WakeUpCommand, command)
                  ):
                      last_heartbeat = trial_runner.send_heartbeat()
                  else:
                      _logger.error("Unsupported command: %s", command)
  
                  trial_runner.save_checkpoint()
  
                  # Reset heartbeat interval to communicate more frequently
                  heartbeat_interval = interval
  
              # No sleep. Continue to next command.
  
          else:
              elapsed = time.time() - last_good
              _logger.info(
                  "No command received. Since last receiving: %f seconds (%f maximum).",
                  elapsed,
                  patience,
              )
  
              if elapsed > patience:
                  _logger.warning(
                      "No command received for too long. Quit the runner."
                  )
                  break
  
              if time.time() - last_heartbeat > heartbeat_interval:
                  last_heartbeat = trial_runner.send_heartbeat()
                  # Exponentially increase heartbeat interval
                  heartbeat_interval = heartbeat_interval * 1.5
  
              time.sleep(interval)
  
imports: from __future__ import annotations
  import atexit
  import argparse
  import json
  import logging
  import os
  import subprocess
  import sys
  import threading
  import time
  from collections import deque
  from functools import partial
  from http.server import BaseHTTPRequestHandler, HTTPServer
  from pathlib import Path
  from typing import Sequence, Callable
  from .file_channel import FileChannel
  from .typehint import CreateCommand, KillCommand, MetricCommand, TrialStatusCommand, WakeUpCommand, ReportAwakeCommand, Trial, Status, typed_dict_validation
  from .utils import graceful_kill, add_handler
label_desc: Use image node_id 1 for calling a global function with example usage: trial_runner_loop(channel, out, rank, interval, patience, log_buffer_size) without return types