BUG pythonw vs subprocess

R

Robin Becker

There seems to be a problem with calling subprocesses from a script run
with pythonw rather than python. The error doesn't seem to be a function
of using pythonw.exe rather than python.exe in the Popen call, but we
seem to get an error when pythonw is used to execute the script proc0.py

C:\Tmp>cat proc0.py
#proc0.py
import sys
from subprocess import Popen, PIPE
sys.stderr = sys.stdout = open('out.txt','w')
job = Popen('pythonw.exe -u proc1.py',stdout=PIPE,stderr=PIPE)
print 'pid',job.pid
out,err=job.communicate()
print 'out', out
print 'err', err
#end of proc0.py

C:\Tmp>cat proc1.py
#proc1.py
import sys, os
print 'sys.executable', sys.executable
print 'sys.argv', sys.argv
print 'stdout IN THE CHILD', os.getpid()
print >>sys.stderr, 'stderr IN THE CHILD', os.getpid()
#end of proc1

C:\Tmp>python proc0.py

C:\Tmp>cat out.txt
pid 1156
out sys.executable c:\python\pythonw.exe
sys.argv ['proc1.py']
stdout IN THE CHILD 1156

err stderr IN THE CHILD 1156


C:\Tmp>pythonw proc0.py

C:\Tmp>cat out.txt
Traceback (most recent call last):
File "proc0.py", line 5, in ?
job = Popen('pythonw.exe -u proc1.py',stdout=PIPE,stderr=PIPE)
File "c:\python\lib\subprocess.py", line 549, in __init__
(p2cread, p2cwrite,
File "c:\python\lib\subprocess.py", line 609, in _get_handles
p2cread = self._make_inheritable(p2cread)
File "c:\python\lib\subprocess.py", line 650, in _make_inheritable
DUPLICATE_SAME_ACCESS)
WindowsError: [Errno 6] The handle is invalid

C:\Tmp>
 
P

Paul Rubin

I thought pythonw didn't provide a console and so it could be that
stdin and stdout aren't connected to anything. Popen therefore doesn't
make sense. You have to use sockets or something.
 
T

Tim Roberts

Paul Rubin said:
I thought pythonw didn't provide a console and so it could be that
stdin and stdout aren't connected to anything. Popen therefore doesn't
make sense. You have to use sockets or something.

Exactly right. The error message is quite accurate: non-console Win32 apps
don't have stdin and stdout. The handles are invalid.
 
J

John J. Lee

Paul Rubin said:
I thought pythonw didn't provide a console and so it could be that
stdin and stdout aren't connected to anything. Popen therefore doesn't
make sense. You have to use sockets or something.

Yeah... I don't know about module subprocess, but I recall there's a
known bug where pythonw actually crashes on win9x when you write to
sys.stdout, since it's not connected to anything...


John
 
R

Robin Becker

John said:
Yeah... I don't know about module subprocess, but I recall there's a
known bug where pythonw actually crashes on win9x when you write to
sys.stdout, since it's not connected to anything...


John
So then it's not possible to get pythonw apps eg tkinter guis to use
subprocess properly? Seems a bit daft to me.
 
R

Robin Becker

Paul said:
I thought pythonw didn't provide a console and so it could be that
stdin and stdout aren't connected to anything. Popen therefore doesn't
make sense. You have to use sockets or something.

Well my example is explicitly using PIPE for stderr & stdin, but I don't know
how to tell it to ignore stdin. The value None seems to be used to signal that
the existing stdin is used and that fails. I believe the solution is just to use
PIPE for stdin as well and then I don't have to write to it.
 
T

Tim Roberts

Robin Becker said:
So then it's not possible to get pythonw apps eg tkinter guis to use
subprocess properly? Seems a bit daft to me.

There's no absolute requirement that a tkinter app use pythonw. If you
call them with python.exe, they'll get a stdin and stdout.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top