[Python-Dev] os.kill() on Win32 ? (Fork on Win32)

M.-A. Lemburg mal@lemburg.com
Mon, 31 Jul 2000 11:12:48 +0200


Fredrik Lundh wrote:
> 
> mal wrote:
> > BTW, (pardon my ignorance) what is the most portable way to
> > do the equivalent of a os.system("cmd &") as native OS
> > API call ? [On Unix, "cmd &" starts a new process which runs
> > in the background and detached from the calling process.]
> >
> > I've looked at .execve and .spawnve, but they both replace
> > the current process.
> 
> on windows, spawn(P_NOWAIT) does what you want.  here's
> an example from the eff-bot guide:
> 
> #
> # os-spawn-example-2.py
> 
> import os
> import string
> 
> def run(program, *args, **kw):
>     # find executable
>     mode = kw.get("mode", os.P_WAIT)
>     for path in string.split(os.environ["PATH"], os.pathsep):
>         file = os.path.join(path, program) + ".exe"
>         try:
>             return os.spawnv(mode, file, (file,) + args)
>         except os.error:
>             pass
>     raise os.error, "cannot find executable"
> 
> run("python", "hello.py", mode=os.P_NOWAIT)

Cool, so os.spawnve(os.P_NOWAIT, ...) looks like a portable
alternative to os.fork() for the case where you do not rely
on the parent process resources being available 
in the child process.

Next, I'll have to find out how to kill a process given
its process ID under Windows... wouldn't it be possible to
write an emulation of os.kill() for Win32 platforms too ?
(there's a SysInfo tool for Windows which says that
OpenProcess(PROCESS_TERMINATE, FALSE, pid); will do the
trick -- not sure if that works as expected though).

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/