FAQ
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

Search Discussions

  • MRAB at Apr 14, 2009 at 4:52 pm

    giohappy wrote:
    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]
  • Giohappy at Apr 15, 2009 at 7:50 am

    On 14 Apr, 18:52, MRAB 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()[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")
  • Kushal Kumaran at Apr 15, 2009 at 9:20 am

    On Wed, Apr 15, 2009 at 1:20 PM, giohappy wrote:
    On 14 Apr, 18:52, MRAB 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()[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.

    --
    kushal
  • Giohappy at Apr 15, 2009 at 9:38 am

    On 15 Apr, 11:20, Kushal Kumaran wrote:
    On Wed, Apr 15, 2009 at 1:20 PM,giohappywrote:
    On 14 Apr, 18:52, MRAB 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()[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.

    --
    kushal
    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/
  • Giohappy at Apr 15, 2009 at 10:24 am

    On 15 Apr, 11:38, giohappy wrote:
    On 15 Apr, 11:20, Kushal Kumaran wrote:


    On Wed, Apr 15, 2009 at 1:20 PM,giohappywrote:
    On 14 Apr, 18:52, MRAB 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()[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.
    --
    kushal
    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...

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 14, '09 at 4:08p
activeApr 15, '09 at 10:24a
posts6
users3
websitepython.org

3 users in discussion

Giohappy: 4 posts MRAB: 1 post Kushal Kumaran: 1 post

People

Translate

site design / logo © 2022 Grokbase