Sending <Ctrl-C> to a process opened from popen()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc

    Sending <Ctrl-C> to a process opened from popen()

    Hi all,

    In searching for an answer to this problem I found several solutions
    dealing with Unix but none dealing with Windows. Basically I want to
    send a ping for an indefinite period of time, stop it at some point,
    and then read the results. I open up a ping process using popen().

    I found ways to send a break signal using signal module, but for that
    I need the PID. All the ways described in the newsgroups for finding
    PID were for UNIX.

    So how do I find the PID of a spawned process in Windows to send a
    break signal?

    Thanks ahead of time,
    Marc
  • Trent Mick

    #2
    Re: Sending &lt;Ctrl-C&gt; to a process opened from popen()

    [Marc wrote][color=blue]
    > Hi all,
    >
    > In searching for an answer to this problem I found several solutions
    > dealing with Unix but none dealing with Windows. Basically I want to
    > send a ping for an indefinite period of time, stop it at some point,
    > and then read the results. I open up a ping process using popen().
    >
    > I found ways to send a break signal using signal module, but for that
    > I need the PID. All the ways described in the newsgroups for finding
    > PID were for UNIX.
    >
    > So how do I find the PID of a spawned process in Windows to send a
    > break signal?[/color]

    You want to use the Win32 API method GenerateConsole CtrlEvent:



    The PyWin32 extensions (comes with ActivePython, can be installed
    separately on other Python distros) provide access to this:

    win32api.Genera teConsoleCtrlEv ent(win32con.CT RL_C_EVENT, pid)

    But you need to get the pid for that. You already have the PID if you
    started a process with CreateProcess, but you are using popen, which
    hides those details. I don't know an easy way to get the PID.

    I have a process.py module which will spawn a process and return a
    object represent it with attributes for stdin/stdout/stderr and methods
    to .wait(), .kill(). This might be more helpful to you, but my
    process.py is not perfect by any stretch.


    Cheers,
    Trent


    --
    Trent Mick
    TrentM@ActiveSt ate.com

    Comment

    Working...