code: def run_subprocess(
      command: list[str], log_file: Path, timeout: float | None = None
  ) -> tuple[int, str, str]:
      if timeout:
          _logger.info(
              "Running command with timeout %f seconds: %s",
              timeout,
              command,
          )
      else:
          _logger.info("Running command: %s", command)
      _logger.info("Output saved to: %s", log_file)
  
      stdout, stderr = "", ""
      file_handle = None
      try:
          start_time = time.time()
          file_handle = log_file.open("w")
          file_handle.write(f"Command: {command}")
          proc = Popen(
              command,
              stdout=PIPE,
              stderr=PIPE,
              encoding="utf-8",
          )
  
          while True:
              out, err = proc.communicate(timeout=1)
              if out:
                  sys.stdout.write(out)
                  sys.stdout.flush()
                  file_handle.write(out)
                  stdout += out
              if err:
                  sys.stderr.write(err)
                  sys.stderr.flush()
                  file_handle.write(err)
                  stderr += err
              file_handle.flush()
  
              # See if the process has terminated
              if proc.poll() is not None:
                  returncode = proc.returncode
                  if returncode != 0:
                      _logger.error(
                          "Command failed with return code %d: %s",
                          returncode,
                          command,
                      )
                  else:
                      _logger.info(
                          "Command finished with return code %d: %s",
                          returncode,
                          command,
                      )
                  return returncode, stdout, stderr
  
              # See if we timed out
              if (
                  timeout is not None
                  and time.time() - start_time > timeout
              ):
                  _logger.warning(
                      "Command timed out (%f seconds): %s",
                      timeout,
                      command,
                  )
                  returncode = graceful_kill(proc)
                  if returncode is None:
                      _logger.error(
                          "Return code is still none after attempting to kill it. The process (%d) may be stuck.",
                          proc.pid,
                      )
                      returncode = 1
                  return returncode, stdout, stderr
      finally:
          if file_handle is not None:
              file_handle.close()
  
file_path: nni/nni/contrib/training_service/utils.py
imports: from __future__ import annotations
  import logging
  import signal
  import sys
  import time
  from pathlib import Path
  from subprocess import Popen, PIPE
label_desc: Use image node_id 2 for calling a global function with example usage: run_subprocess(command, log_file, timeout) and returns: returncode,  stdout,  stderr, returncode,  stdout,  stderr