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

我有一个Python脚本,它作为Windows服务运行.该脚本分叉另一个进程:
with subprocess.Popen( args=[self.exec_path],stdout=subprocess.PIPE,stderr=subprocess.STDOUT) as proc:

这会导致以下错误

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
subprocess.py中的第1117行是:
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)

这让我怀疑服务流程没有与之关联的STDIN(TBC)

通过向popen提供文件或空设备作为stdin参数,可以避免这种麻烦的代码.

在Python 3.x中,您可以简单地传递stdin = subprocess.DEVNULL.例如.

subprocess.Popen( args=[self.exec_path],stderr=subprocess.STDOUT,stdin=subprocess.DEVNULL)

在Python 2.x中,您需要将文件处理程序设置为null,然后将其传递给popen:

devnull = open(os.devnull,'wb')
subprocess.Popen( args=[self.exec_path],stdin=devnull)

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...