safe_subprocess.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:Deploy_XXNET_Server 作者: jzp820927 项目源码 文件源码
def start_process(args, input_string='', env=None, cwd=None, stdout=None,
                  stderr=None):
  """Starts a subprocess like subprocess.Popen, but is threadsafe.

  The value of input_string is passed to stdin of the subprocess, which is then
  closed.

  Args:
    args: A string or sequence of strings containing the program arguments.
    input_string: A string to pass to stdin of the subprocess.
    env: A dict containing environment variables for the subprocess.
    cwd: A string containing the directory to switch to before executing the
        subprocess.
    stdout: A file descriptor, file object or subprocess.PIPE to use for the
        stdout descriptor for the subprocess.
    stderr: A file descriptor, file object or subprocess.PIPE to use for the
        stderr descriptor for the subprocess.

  Returns:
    A subprocess.Popen instance for the created subprocess.
  """
  with _popen_lock:
    logging.debug('Starting process %r with input=%r, env=%r, cwd=%r',
                  args, input_string, env, cwd)

    # Suppress the display of the console window on Windows.
    # Note: subprocess.STARTF_USESHOWWINDOW & subprocess.SW_HIDE are only
    # availalbe after Python 2.7.2 on Windows.
    if (hasattr(subprocess, 'SW_HIDE') and
        hasattr(subprocess, 'STARTF_USESHOWWINDOW')):
      startupinfo = subprocess.STARTUPINFO()
      startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
      startupinfo.wShowWindow = subprocess.SW_HIDE
    else:
      startupinfo = None

    p = subprocess.Popen(args, env=env, cwd=cwd, stdout=stdout, stderr=stderr,
                         stdin=subprocess.PIPE, startupinfo=startupinfo)
    if _SUBPROCESS_STDIN_IS_THREAD_HOSTILE:
      p.stdin.write(input_string)
      p.stdin.close()
      p.stdin = None
  if not _SUBPROCESS_STDIN_IS_THREAD_HOSTILE:
    p.stdin.write(input_string)
    p.stdin.close()
    p.stdin = None
  return p
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号