pausing or killing a process
news at snakefarm.org
Tue Feb 4 23:49:37 CET 2003
David Martens wrote:
> Is there a reasonable way of pausing a process
> within Python?
Send it a SIGSTOP:
It will pause until it gets a SIGCONT:
> Additionally, a process can be killed by doing a
> os.system("kill -9 %i" % pid) but that falls short
> of elegant. Can it be done better?
But you should send a SIGTERM first to give the process a chance to exit
cleanly. Then perhaps wait some time and finally really kill it:
# send SIGTERM
# give time to finish
# kill it (beware of exceptions!)
Instead of a dumb sleep() you could wait until the process really
finishes. If the process has been forked within the program, loop over
os.waitpid(pid, os.WNOHANG) as long as you feel appropriate. It will
return the pid once the child exits. If the process is not a child of
your program, but the owners are the same, check by sending signal 0
(os.kill(pid, 0)). This will raise an exception if the child is not there.
More information about the Python-list