code: def _thread_process_runner(
      run: LocalSubmittedRun,
      args: List[str],
      work_dir: str,
      env: Dict[str, str],
  ) -> None:
      # cancel was called before we started the subprocess
      if run._terminate_flag:
          return
      # TODO: Make this async
      process = subprocess.Popen(
          args,
          close_fds=True,
          stdout=subprocess.PIPE,
          stderr=subprocess.STDOUT,
          universal_newlines=True,
          bufsize=1,
          cwd=work_dir,
          env=env,
      )
      run.set_command_proc(process)
      run._stdout = ""
      while True:
          # the agent thread could set the terminate flag
          if run._terminate_flag:
              process.terminate()  # type: ignore
          chunk = os.read(process.stdout.fileno(), 4096)  # type: ignore
          if not chunk:
              break
          index = chunk.find(b"\r")
          decoded_chunk = chunk.decode()
          if index != -1:
              run._stdout += decoded_chunk
              print(chunk.decode(), end="")
          else:
              run._stdout += decoded_chunk + "\r"
              print(chunk.decode(), end="\r")
  
imports: import asyncio
  import logging
  import os
  import shlex
  import subprocess
  import sys
  import threading
  from typing import TYPE_CHECKING, Any, Dict, List, Optional
  import wandb
  from wandb.sdk.launch.environment.abstract import AbstractEnvironment
  from wandb.sdk.launch.registry.abstract import AbstractRegistry
  from .._project_spec import LaunchProject
  from ..builder.build import get_env_vars_dict
  from ..errors import LaunchError
  from ..utils import LOG_PREFIX, MAX_ENV_LENGTHS, PROJECT_SYNCHRONOUS, _is_wandb_dev_uri, _is_wandb_local_uri, docker_image_exists, event_loop_thread_exec, pull_docker_image, sanitize_wandb_api_key
  from .abstract import AbstractRun, AbstractRunner, Status
label_desc: Use image node_id 2 for calling a global function with example usage: _thread_process_runner(run, args, work_dir, env) without return types