I have a Python script, which is running as a Windows Service. The script forks another process with:
JavaScript
x
2
1
with subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
2
which causes the following error:
JavaScript
1
4
1
OSError: [WinError 6] The handle is invalid
2
File "C:Program Files (x86)Python35-32libsubprocess.py", line 911, in __init__
3
File "C:Program Files (x86)Python35-32libsubprocess.py", line 1117, in _get_handles
4
Advertisement
Answer
Line 1117 in subprocess.py
is:
JavaScript
1
2
1
p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
2
which made me suspect that service processes do not have a STDIN associated with them (TBC)
This troublesome code can be avoided by supplying a file or null device as the stdin argument to popen
.
In Python 3.x, you can simply pass stdin=subprocess.DEVNULL
. E.g.
JavaScript
1
2
1
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
2
In Python 2.x, you need to get a filehandler to null, then pass that to popen:
JavaScript
1
3
1
devnull = open(os.devnull, 'wb')
2
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
3