作为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], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)

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

 

devnull = open(os.devnull, 'wb')
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值