subprocess: reading from stdout hangs process termination, waiting for ENTER keyboard signal
giohappy at gmail.com
Wed Apr 15 11:38:09 CEST 2009
On 15 Apr, 11:20, Kushal Kumaran <kushal.kuma... at gmail.com> wrote:
> On Wed, Apr 15, 2009 at 1:20 PM,giohappy<gioha... at gmail.com> wrote:
> > On 14 Apr, 18:52, MRAB <goo... at mrabarnett.plus.com> wrote:
> >> giohappywrote:
> >> > Hello everyone.
> >> > I'm trying to use subprocess module to launch a Windows console
> >> > application. The application prints some results to standard output
> >> > and then waits for the user to press any key to terminte. I can't
> >> > control this behaviour, as the application is not mine...
> >> > I'm stuck at the very first lines of my code. I'm trying to force
> >> > process termination (even with proc.terminate()), and it works only if
> >> > I don't read from stdout. If I do proc.stdout.read() the process
> >> > hangs, and I have to manually press the keyboard to interrupt it.
> >> > Probably it's due a low-level handle that is kept on the process
> >> > stdout, waiting for the keypress event...
> >> > How can I solve it?
> >> > Giovanni
> >> > ------- Code excerpt-------
> >> > proc = subprocess.Popen('the_app.exe',
> >> > shell=True,
> >> > stdout=subprocess.PIPE,
> >> > )
> >> > #stdout_value = proc.communicate()
> >> > stdout_value = proc.stdout.read()
> >> > PROCESS_TERMINATE = 1
> >> > handle = win32api.OpenProcess(PROCESS_TERMINATE, False, proc.pid)
> >> > win32api.TerminateProcess(handle, -1)
> >> > win32api.CloseHandle(handle)
> >> > print stdout_value
> >> Try this:
> >> proc = subprocess.Popen('the_app.exe',
> >> shell=True,
> >> stdin=subprocess.PIPE,
> >> stdout=subprocess.PIPE,
> >> )
> >> stdout_value = proc.communicate("\n")
> > MRAB, I've tried that too but no result... I still have to press a
> > keybord key to terminate (the classical "Press any key to continue")
> If it actually is "Press any key to continue" rather than "Press Enter
> to continue", it is likely directly using the console using available
> low-level APIs, rather than reading from stdin. AFAIK, subprocess
> cannot handle that.
If also tried with SendKeys , wich uses the windows.h keybd_event
(), but it doesn't work... Ok, I leave this try, and look for a way to
wrap the application in a bat file, hoping to succesfuly simulate the
keypress event inside it.
More information about the Python-list