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

Discussion in 'Python' started by Marc, Aug 20, 2003.

  1. Marc

    Marc Guest

    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, Aug 20, 2003
    1. Advertisements

  2. Marc

    Trent Mick Guest

    [Marc wrote]
    You want to use the Win32 API method GenerateConsoleCtrlEvent:

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

    win32api.GenerateConsoleCtrlEvent(win32con.CTRL_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 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 is not perfect by any stretch.

    Trent Mick, Aug 20, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.