Python Windows上GUI应用程序中的子进程失败
在运行使用pyinstaller--noconsole创建的python windows exe时调用子进程Popen、call或check_输出会导致以下堆栈出错:Python Windows上GUI应用程序中的子进程失败,python,python-2.7,Python,Python 2.7,在运行使用pyinstaller--noconsole创建的python windows exe时调用子进程Popen、call或check_输出会导致以下堆栈出错: ... File "subprocess.py", line 566, in check_output File "subprocess.py", line 702, in __init__ File "subprocess.py", line 823, in _get_handles WindowsError: [Error 6
...
File "subprocess.py", line 566, in check_output
File "subprocess.py", line 702, in __init__
File "subprocess.py", line 823, in _get_handles
WindowsError: [Error 6] The handle is invalid
遇到但没有直接的答案,所以创建此帖子希望能让解决方案更容易找到 我找到了阿拉斯泰尔·麦科马克的答案(谢谢!!!!)。问题是子流程无法正确解析默认的stdin/out
为后代复制:
subprocess.py
中的第1117行是:
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
这让我怀疑服务进程没有与之关联的STDIN(TBC)
通过将文件或空设备作为stdin参数提供给popen
,可以避免这种麻烦的代码
在Python 3.3、3.4和3.5中,您只需传递stdin=subprocess.DEVNULL
。例如
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
在Python 2.x中,需要将filehandler设置为null,然后将其传递给popen:
devnull = open(os.devnull, 'wb')
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)