code: def running_subprocess(
      command, after="wait", verbose=None, *args, **kwargs
  ):
      """Context manager to do something with a command running via Popen.
  
      Parameters
      ----------
      command : list of str | str
          Command to run as subprocess (see :class:`python:subprocess.Popen`).
      after : str
          Can be:
  
          - "wait" to use :meth:`~python:subprocess.Popen.wait`
          - "communicate" to use :meth:`~python.subprocess.Popen.communicate`
          - "terminate" to use :meth:`~python:subprocess.Popen.terminate`
          - "kill" to use :meth:`~python:subprocess.Popen.kill`
  
      %(verbose)s
      *args, **kwargs : arguments
          Additional arguments to pass to subprocess.Popen.
  
      Returns
      -------
      p : instance of Popen
          The process.
      """
      _validate_type(after, str, "after")
      _check_option(
          "after", after, ["wait", "terminate", "kill", "communicate"]
      )
      contexts = list()
      for stdxxx in ("stderr", "stdout"):
          if stdxxx not in kwargs:
              kwargs[stdxxx] = subprocess.PIPE
              contexts.append(stdxxx)
  
      # Check the PATH environment variable. If run_subprocess() is to be called
      # frequently this should be refactored so as to only check the path once.
      env = kwargs.get("env", os.environ)
      if any(p.startswith("~") for p in env["PATH"].split(os.pathsep)):
          warn(
              "Your PATH environment variable contains at least one path "
              'starting with a tilde ("~") character. Such paths are not '
              "interpreted correctly from within Python. It is recommended "
              'that you use "$HOME" instead of "~".'
          )
      if isinstance(command, str):
          command_str = command
      else:
          command = [str(s) for s in command]
          command_str = " ".join(s for s in command)
      logger.info("Running subprocess: %s" % command_str)
      try:
          p = subprocess.Popen(command, *args, **kwargs)
      except Exception:
          if isinstance(command, str):
              command_name = command.split()[0]
          else:
              command_name = command[0]
          logger.error("Command not found: %s" % command_name)
          raise
      try:
          with ExitStack() as stack:
              for context in contexts:
                  stack.enter_context(getattr(p, context))
              yield p
      finally:
          getattr(p, after)()
          p.wait()
  
imports: import fnmatch
  import gc
  import hashlib
  import inspect
  import os
  import subprocess
  import sys
  import traceback
  import weakref
  from contextlib import ExitStack, contextmanager
  from importlib.resources import files
  from math import log
  from queue import Empty, Queue
  from string import Formatter
  from textwrap import dedent
  from threading import Thread
  import numpy
  from decorator import FunctionMaker
  from ._logging import logger, verbose, warn
  from .check import _check_option, _validate_type
label_desc: Use image node_id 8 for calling a global function with example usage: running_subprocess(command, after, verbose) without return types