subprocess: reading from stdout hangs process termination, waitingfor ENTER keyboard signal

G

giohappy

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()[0]
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
 
M

MRAB

giohappy said:
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()[0]
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")[0]
 
G

giohappy

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()[0]
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")[0]

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")
 
K

Kushal Kumaran

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()[0]
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")[0]

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.
 
G

giohappy

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()[0]
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")[0]
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 [1], 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.

[1] http://www.rutherfurd.net/python/sendkeys/
 
G

giohappy

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()[0]
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")[0]
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 [1], 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.

[1]http://www.rutherfurd.net/python/sendkeys/

Sorry, SendKeys is the solution. I was using it in the wronk place in
my script...
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top