Ala*_*ack 13 python windows subprocess
我有一个Python脚本,它作为Windows服务运行.该脚本分叉另一个进程:
with subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
OSError: [WinError 6] The handle is invalid
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 911, in __init__
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 1117, in _get_handles
Run Code Online (Sandbox Code Playgroud)
Ala*_*ack 21
1117行subprocess.py是:
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
Run Code Online (Sandbox Code Playgroud)
这让我怀疑服务流程没有与他们相关的STDIN(TBC)
通过提供文件或空设备作为stdin参数,可以避免这种麻烦的代码popen.
在Python 3.x中,您可以简单地传递stdin=subprocess.DEVNULL.例如
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
Run Code Online (Sandbox Code Playgroud)
在Python 2.x中,您需要将文件处理程序设置为null,然后将其传递给popen:
devnull = open(os.devnull, 'wb')
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
Run Code Online (Sandbox Code Playgroud)