code: def copy_process_streams(
      process: subprocess.Popen,
      stdout: io.StringIO,
      stderr: io.StringIO,
      verbose: bool = False,
  ):
      """
      Reads the given process stdout/stderr and write them to StringIO objects.
      Make sure that there is no deadlock because of pipe congestion.
      If `verbose` the process stdout/stderr are also copying to the interpreter stdout/stderr.
      """
  
      def raw(stream: tp.Optional[tp.IO[bytes]]) -> tp.IO[bytes]:
          assert stream is not None
          if isinstance(stream, io.BufferedIOBase):
              stream = stream.raw
          return stream
  
      p_stdout, p_stderr = raw(process.stdout), raw(process.stderr)
      stream_by_fd: tp.Dict[
          int, tp.Tuple[tp.IO[bytes], io.StringIO, tp.IO[str]]
      ] = {
          p_stdout.fileno(): (p_stdout, stdout, sys.stdout),
          p_stderr.fileno(): (p_stderr, stderr, sys.stderr),
      }
      fds = list(stream_by_fd.keys())
      poller = select.poll()
      for fd in stream_by_fd:
          poller.register(fd, select.POLLIN | select.POLLPRI)
      while fds:
          # `poll` syscall will wait until one of the registered file descriptors has content.
          ready = poller.poll()
          for fd, _ in ready:
              p_stream, string, std = stream_by_fd[fd]
              raw_buf = p_stream.read(2**16)
              if not raw_buf:
                  fds.remove(fd)
                  poller.unregister(fd)
                  continue
              buf = raw_buf.decode()
              string.write(buf)
              string.flush()
              if verbose:
                  std.write(buf)
                  std.flush()
  
imports: import contextlib
  import io
  import itertools
  import os
  import pickle
  import select
  import shutil
  import subprocess
  import sys
  import tarfile
  import typing
  from pathlib import Path
  import cloudpickle
label_desc: Use image node_id 7 for calling a global function with example usage: copy_process_streams(process, stdout, stderr, verbose) and returns: stream