code: def kill_process(ip, port, pids):
      """ssh to a remote machine and kill the specified processes."""
      curr_pid = os.getpid()
      killed_pids = []
      # If we kill child processes first, the parent process may create more again. This happens
      # to Python's process pool. After sorting, we always kill parent processes first.
      pids.sort()
      for pid in pids:
          assert curr_pid != pid
          print(
              "kill process {} on {}:{}".format(pid, ip, port),
              flush=True,
          )
          kill_cmd = (
              "ssh -o StrictHostKeyChecking=no -p "
              + str(port)
              + " "
              + ip
              + " 'kill {}'".format(pid)
          )
          subprocess.run(kill_cmd, shell=True)
          killed_pids.append(pid)
      # It's possible that some of the processes are not killed. Let's try again.
      for i in range(3):
          killed_pids = get_killed_pids(ip, port, killed_pids)
          if len(killed_pids) == 0:
              break
          else:
              killed_pids.sort()
              for pid in killed_pids:
                  print(
                      "kill process {} on {}:{}".format(pid, ip, port),
                      flush=True,
                  )
                  kill_cmd = (
                      "ssh -o StrictHostKeyChecking=no -p "
                      + str(port)
                      + " "
                      + ip
                      + " 'kill -9 {}'".format(pid)
                  )
                  subprocess.run(kill_cmd, shell=True)
  
imports: import argparse
  import json
  import logging
  import multiprocessing
  import os
  import re
  import signal
  import stat
  import subprocess
  import sys
  import time
  from functools import partial
  from threading import Thread
  from typing import Optional
label_desc: Use image node_id 2 for calling a global function with example usage: kill_process(ip, port, pids) without return types