windows code to detect if a process is still alive

Kuno Meyer kuno.meyer at gmx.ch
Thu Apr 14 15:06:07 UTC 2011


Martin Pool <mbp <at> canonical.com> writes:

> For detection of stale lock files, I want to detect whether a given
> process id on the local machine still corresponds to a live process.
> On unix, kill(pid, 0) will do this.  What's the best way to do this
> from inside Python on Windows?

You could use the OpenProcess (see [1]) and similar Win32 API methods, which are
to some extent accessible through Mark Hammonds Win32 wrappers:

    import win32api
    import win32process

    PROCESS_QUERY_INFORMATION = 0x0400

    pid = win32api.GetCurrentProcessId()
    h = win32api.OpenProcess(PROCESS_QUERY_INFORMATION, False, pid)

    print win32process.GetProcessTimes(h)

Kuno





More information about the bazaar mailing list