[Python-Dev] RE: how to kill process on Windows started with os.spawn?

Tim Peters tim_one@email.msn.com
Sun, 1 Dec 2002 10:43:41 -0500


[Skip Montanaro]
> Regarding how to kill a process on Windows which was started with
> os.spawn*, on the Spambayes list I asked:
>
>     >> How would I kill that process on Windows,
>
> Mark Hammond replied:
>
>     This worked for me just then:
>
>     >>> os.spawnl(os.P_NOWAIT, "f:\\windows\\notepad.exe")
>     548
>     >>> import win32api
>     >>> win32api.TerminateProcess(548,0)
>
> Tim also replied:
>
>     Tim> For 2.3, I implemented something or other for the Python core;
>     Tim> probably killpid();

I didn't remember correctly -- I implemented waitpid() for Windows.
Processes on Windows aren't meant to be externally killed, and the docs have
dire warnings about use of TerminateProcess.  Not that people don't use it,
but it's dangerous.

> Shouldn't one of these solutions (or some variant) appear under the
> veneer of os.kill()?

Signals make little sense on Windows.

> I realize the semantics might not be exactly the same
> (perhaps Windows solutions don't support different signals

Mone -- the second argument to TerminateProcess isn't a signal number, it's
the exit code to be returned by the terminated process.

> or a different set of signals),

Nope, none.

> but it seems to me that if an attempt is made to provide
> the os.spawn* functions across platforms we should make a similar
> attempt to provide a way to kill spawned processes as well.

Be my guest <wink>.

> Corollary question: Is there some reason the win32api stuff isn't in
> the core?

I *expect* Guido would like that, provided the code fit the Python style
guide and the docs were bashed into LaTeX.  That's a massive amount of
no-fun work, and I don't expect Mark to volunteer for either.