作为Windows服务运行的Python:OSError:[WinError 6]句柄无效

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)

  • 好吧,我想我在这里有最边缘的情况,所以我不确定其他人会发现它 - 我的脚本已经使用PyInstaller编译成一个独立的.exe.该脚本不是实际服务 - C#/ .net应用程序是支持独立.exe的服务.该服务具有独立模式,当作为桌面用户执行时,可以按预期工作. (3认同)
  • 通过 pythonw.exe 运行时,类似的错误很常见。在 Windows 8 之前,pythonw.exe 进程在其标准句柄中有控制台句柄值,但它们无效,因为没有附加控制台。当子进程尝试在无效句柄上调用“DuplicateHandle”时,它会引发错误。 (2认同)
  • 在嵌套(subprocess.Popen,Windows 应用程序,通过 DLL 的 python)运行时,Python 3.6 中出现类似的错误,用于 `subprocess.run(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE)`。添加`stdin = subprocess.DEVNULL` 修复了它。 (2认同)