code: def run_and_monitor(args, pid_to_wait, std_out_filter_fn=None, cwd=None):
    
  
    monitor_process = None
    try:
      p = subprocess.Popen(args,
                           cwd=cwd,
                           env=os.environ,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT)
  
      pids_to_kill = [p.pid]
      script = ('import %s;%s._wait_and_kill(%s, %s)' %
                (__name__, __name__, str(pid_to_wait), str(pids_to_kill)))
      monitor_process = subprocess.Popen(['python', '-c', script], env=os.environ)
      while p.poll() is None:
        line = p.stdout.readline()
  
        if not six.PY2:
          line = line.decode()
  
        if std_out_filter_fn is None or std_out_filter_fn(line):
          sys.stdout.write(line)
          # Cannot do sys.stdout.flush(). It appears that too many flush() calls will hang browser.
    finally:
      if monitor_process:
        monitor_process.kill()
docs: Start a process, and have it depend on another specified process.
  
    Args:
      args: the args of the process to start and monitor.
      pid_to_wait: the process to wait on. If the process ends, also kill the started process.
      std_out_filter_fn: a filter function which takes a string content from the stdout of the
          started process, and returns True if the string should be redirected to console stdout.
      cwd: the current working directory for the process to start.