code: def run(self):
      # redirect trial's stdout and stderr to syslog
      self.trial_syslogger_stdout = RemoteLogger(
          self.args.nnimanager_ip,
          self.args.nnimanager_port,
          "trial",
          StdOutputType.Stdout,
          self.args.log_collection,
          self.id,
          self.args.command_channel,
      )
  
      nni_log(LogType.Info, "%s: start to run trial" % self.name)
  
      trial_working_dir = os.path.realpath(
          os.path.join(os.curdir, "..", "..", "trials", self.id)
      )
      self.trial_output_dir = os.path.join(
          trial_working_dir, trial_output_path_name
      )
      trial_code_dir = os.path.join(trial_working_dir, "code")
      trial_nnioutput_dir = os.path.join(trial_working_dir, "nnioutput")
  
      environ = os.environ.copy()
      environ["NNI_TRIAL_SEQ_ID"] = str(self.data["sequenceId"])
      environ["NNI_OUTPUT_DIR"] = os.path.join(
          trial_working_dir, "nnioutput"
      )
      environ["NNI_SYS_DIR"] = trial_working_dir
      self.working_dir = trial_working_dir
  
      # prepare code and parameters
      prepared_flag_file_name = os.path.join(
          trial_working_dir, "trial_prepared"
      )
      if not os.path.exists(trial_working_dir):
          os.makedirs(trial_working_dir, exist_ok=True)
  
          os.makedirs(self.trial_output_dir, exist_ok=True)
          os.makedirs(trial_nnioutput_dir, exist_ok=True)
          # prepare code
          os.makedirs(trial_code_dir, exist_ok=True)
          with tarfile.open(
              os.path.join("..", "nni-code.tar.gz"), "r:gz"
          ) as tar:
              tar.extractall(trial_code_dir)
  
          # save parameters
          nni_log(
              LogType.Info,
              "%s: saving parameter %s"
              % (self.name, self.data["parameter"]["value"]),
          )
          parameter_file_name = os.path.join(
              trial_working_dir, "parameter.cfg"
          )
          with open(parameter_file_name, "w") as parameter_file:
              parameter_file.write(self.data["parameter"]["value"])
  
          # ready flag
          with open(prepared_flag_file_name, "w") as prepared_flag_file:
              prepared_flag_file.write(
                  "%s" % (int(datetime.now().timestamp() * 1000))
              )
  
      # make sure code prepared by other node.
      if self.node_id is not None:
          while True:
              if os.path.exists(prepared_flag_file_name):
                  break
              time.sleep(0.1)
  
      trial_command = self.args.trial_command
  
      gpuIndices = self.data.get("gpuIndices")
      if gpuIndices is not None:
          if sys.platform == "win32":
              trial_command = (
                  'set CUDA_VISIBLE_DEVICES="%s " && call %s'
                  % (gpuIndices, trial_command)
              )
          else:
              trial_command = 'CUDA_VISIBLE_DEVICES="%s " %s' % (
                  gpuIndices,
                  trial_command,
              )
  
      self.log_pipe_stdout = (
          self.trial_syslogger_stdout.get_pipelog_reader()
      )
      self.process = Popen(
          trial_command,
          shell=True,
          stdout=self.log_pipe_stdout,
          stderr=self.log_pipe_stdout,
          cwd=trial_code_dir,
          env=dict(environ),
      )
      nni_log(
          LogType.Info,
          "{0}: spawns a subprocess (pid {1}) to run command: {2}".format(
              self.name, self.process.pid, shlex.split(trial_command)
          ),
      )
  
file_path: nni/nni/tools/trial_tool/trial.py
imports: import ctypes
  import os
  import sys
  import shlex
  import tarfile
  import time
  from datetime import datetime
  from subprocess import Popen
  import psutil
  from .log_utils import LogType, RemoteLogger, StdOutputType, nni_log
  from .commands import CommandType
label_desc: Use image node_id 2 for calling the Trial obj's underlying member method code with example usage: obj.run() without return types