def winpopen4(orig, cmd, env=None, newlines=False, bufsize=-1): """Same as util.popen4, but manually creates an input pipe with a larger than default buffer""" import msvcrt if sys.version_info[0] < 3: import _subprocess handles = _subprocess.CreatePipe(None, pipei_bufsize) rfd, wfd = [msvcrt.open_osfhandle(h, 0) for h in handles] else: import _winapi handles = _winapi.CreatePipe(None, pipei_bufsize) rfd, wfd = [msvcrt.open_osfhandle(h, 0) for h in handles] handles = [subprocess.Handle(h) for h in handles] handles[0].Detach() handles[1].Detach() p = subprocess.Popen( cmd, shell=True, bufsize=bufsize, close_fds=False, stdin=rfd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=newlines, env=env, ) p.stdin = util.fdopen(wfd, "wb", bufsize) return p.stdin, p.stdout, p.stderr, p
def _pipe(): """Create a unidirectional pipe.""" # use _winapi.CreatePipe on windows, just like subprocess.Popen # does when requesting PIPE streams. This is the easiest and most # reliable method I have tested so far: recv, send = _winapi.CreatePipe(None, 0) return Handle(recv), Handle(send)
def winpopen4(orig, cmd, env=None, newlines=False, bufsize=-1): """Same as util.popen4, but manually creates an input pipe with a larger than default buffer""" import msvcrt # pyre-fixme[21]: Could not find module `_subprocess`. import _subprocess handles = _subprocess.CreatePipe(None, pipei_bufsize) rfd, wfd = [msvcrt.open_osfhandle(h, 0) for h in handles] handles[0].Detach() handles[1].Detach() p = subprocess.Popen( cmd, shell=True, bufsize=bufsize, close_fds=False, stdin=rfd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=newlines, env=env, ) p.stdin = util.fdopen(wfd, "wb", bufsize) return p.stdin, p.stdout, p.stderr, p
def _get_handles(self, stdin, stdout, stderr): """ Construct and return tuple with IO objects: p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite """ if stdin is None and stdout is None and stderr is None: return (None, None, None, None, None, None) p2cread, p2cwrite = None, None c2pread, c2pwrite = None, None errread, errwrite = None, None if stdin is None: p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE) if p2cread is None: p2cread, _ = _subprocess.CreatePipe(None, 0) elif stdin == subprocess.PIPE: p2cread, p2cwrite = _subprocess.CreatePipe(None, 0) elif getattr(stdin, "getsockopt", None): p2cread = stdin.fileno() elif isinstance(stdin, int): p2cread = msvcrt.get_osfhandle(stdin) else: # Assuming file-like object p2cread = msvcrt.get_osfhandle(stdin.fileno()) p2cread = self._make_inheritable(p2cread) if stdout is None: c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE) if c2pwrite is None: _, c2pwrite = _subprocess.CreatePipe(None, 0) elif stdout == subprocess.PIPE: c2pread, c2pwrite = _subprocess.CreatePipe(None, 0) elif getattr(stdout, "getsockopt", None): c2pwrite = stdout.fileno() elif isinstance(stdout, int): c2pwrite = msvcrt.get_osfhandle(stdout) else: # Assuming file-like object c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) c2pwrite = self._make_inheritable(c2pwrite) if stderr is None: errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE) if errwrite is None: _, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == subprocess.PIPE: errread, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == subprocess.STDOUT: errwrite = c2pwrite elif isinstance(stderr, int): errwrite = msvcrt.get_osfhandle(stderr) else: # Assuming file-like object errwrite = msvcrt.get_osfhandle(stderr.fileno()) errwrite = self._make_inheritable(errwrite) return (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
def _get_handles(self, stdin, stdout, stderr): """Construct and return tuple with IO objects: p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite """ to_close = set() if stdin is None and stdout is None and stderr is None: return ((None, None, None, None, None, None), to_close) else: p2cread, p2cwrite = (None, None) c2pread, c2pwrite = (None, None) errread, errwrite = (None, None) if stdin is None: p2cread = _subprocess.GetStdHandle( _subprocess.STD_INPUT_HANDLE) if p2cread is None: p2cread, _ = _subprocess.CreatePipe(None, 0) elif stdin == PIPE: p2cread, p2cwrite = _subprocess.CreatePipe(None, 0) elif isinstance(stdin, int): p2cread = msvcrt.get_osfhandle(stdin) else: p2cread = msvcrt.get_osfhandle(stdin.fileno()) p2cread = self._make_inheritable(p2cread) to_close.add(p2cread) if stdin == PIPE: to_close.add(p2cwrite) if stdout is None: c2pwrite = _subprocess.GetStdHandle( _subprocess.STD_OUTPUT_HANDLE) if c2pwrite is None: _, c2pwrite = _subprocess.CreatePipe(None, 0) elif stdout == PIPE: c2pread, c2pwrite = _subprocess.CreatePipe(None, 0) elif isinstance(stdout, int): c2pwrite = msvcrt.get_osfhandle(stdout) else: c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) c2pwrite = self._make_inheritable(c2pwrite) to_close.add(c2pwrite) if stdout == PIPE: to_close.add(c2pread) if stderr is None: errwrite = _subprocess.GetStdHandle( _subprocess.STD_ERROR_HANDLE) if errwrite is None: _, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == PIPE: errread, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == STDOUT: errwrite = c2pwrite elif isinstance(stderr, int): errwrite = msvcrt.get_osfhandle(stderr) else: errwrite = msvcrt.get_osfhandle(stderr.fileno()) errwrite = self._make_inheritable(errwrite) to_close.add(errwrite) if stderr == PIPE: to_close.add(errread) return ((p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite), to_close)
def _get_handles(self, stdin, stdout, stderr): """Construct and return tuple with IO objects: p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite """ to_close = set() if stdin is None and stdout is None and stderr is None: return (None, None, None, None, None, None), to_close p2cread, p2cwrite = None, None c2pread, c2pwrite = None, None errread, errwrite = None, None if stdin is None: p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE) if p2cread is None: p2cread, _ = _subprocess.CreatePipe(None, 0) elif stdin == PIPE: p2cread, p2cwrite = _subprocess.CreatePipe(None, 0) elif isinstance(stdin, (int, long)): p2cread = msvcrt.get_osfhandle(stdin) else: # Assuming file-like object p2cread = msvcrt.get_osfhandle(stdin.fileno()) p2cread = self._make_inheritable(p2cread) # We just duplicated the handle, it has to be closed at the end to_close.add(p2cread) if stdin == PIPE: to_close.add(p2cwrite) if stdout is None: c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE) if c2pwrite is None: _, c2pwrite = _subprocess.CreatePipe(None, 0) elif stdout == PIPE: c2pread, c2pwrite = _subprocess.CreatePipe(None, 0) elif isinstance(stdout, (int, long)): c2pwrite = msvcrt.get_osfhandle(stdout) else: # Assuming file-like object c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) c2pwrite = self._make_inheritable(c2pwrite) # We just duplicated the handle, it has to be closed at the end to_close.add(c2pwrite) if stdout == PIPE: to_close.add(c2pread) if stderr is None: errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE) if errwrite is None: _, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == PIPE: errread, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == STDOUT: errwrite = c2pwrite elif isinstance(stderr, (int, long)): errwrite = msvcrt.get_osfhandle(stderr) else: # Assuming file-like object errwrite = msvcrt.get_osfhandle(stderr.fileno()) errwrite = self._make_inheritable(errwrite) # We just duplicated the handle, it has to be closed at the end to_close.add(errwrite) if stderr == PIPE: to_close.add(errread) return (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite), to_close
import time import os import msvcrt import _subprocess import subprocess def _make_inheritable(handle): """Return a duplicate of handle, which is inheritable""" return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(), handle, _subprocess.GetCurrentProcess(), 0, 1, _subprocess.DUPLICATE_SAME_ACCESS) bufsize = 0 #创建输入接口 pipein1,pipein1w = _subprocess.CreatePipe(None, 0) pipein1 = _make_inheritable(pipein1) #接管进程输入口 startupinfo32 = subprocess.STARTUPINFO() startupinfo32.dwFlags = subprocess.STARTF_USESHOWWINDOW | subprocess.STARTF_USESTDHANDLES startupinfo32.wShowWindow = 1 startupinfo32.hStdInput = pipein1 startupinfo32.hStdOut = _make_inheritable(_subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE)) #显示窗口方式打开新进程 p1 = subprocess.Popen('webrtc-cpp-sample.exe',startupinfo = startupinfo32,creationflags=subprocess.CREATE_NEW_CONSOLE) #关闭句柄 if p1.stdin: p1.stdin.close() if p1.stdout: p1.stdout.close()