You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This would serve the following use case:
* we have a list of processes we want to terminate
* send them SIGTERM (via Process.terminate())
* give them some time to terminate (say 3 seconds)
* figure out which ones are terminated and which ones are not
* for those ones which are not send SIGKILL as last resort (via Process.kill())
* wait again and in case they are still alive just give up
If we'd have to do this for a single process psutil already provides all the
necessary hooks:
p.terminate()
try:
p.wait(timeout=3)
except psutil.TimeoutExpired:
p.kill()
try:
p.wait(timeout=3)
except psutil.TimeoutExpired:
sys.exit('giving up')
When it comes to do the same thing for a *list* of processes it is not
immediately clear how to do this right.
We can introduce a new psutil.wait_procs(plist, timeout, callback=None) utility
function which:
* waits for a list of processes 'plist' to terminate and return a (gone,
still_alive) tuple
* gone and still_alive are lists of Process instances
* Process instances in the 'gone' list will have a new 'retcode' attribute
attached to them
* 'callback' kwarg, if specified, will be called with a 'proc' argument as soon
as a process terminates
Code sample:
def on_terminate(proc):
print("process {} terminated".format(proc))
def on_kill(proc):
print("process {} killed".format(proc))
for p in procs:
p.terminate()
gone, alive = psutil.wait_procs(procs, 3, callback=on_terminate)
if alive:
for p in alive:
p.kill()
gone, alive = psutil.wait_procs(procs, 3, callback=on_kill)
for p in alive:
print("could not kill process {}".format(p))
From g.rodola on October 09, 2013 15:14:54
Original issue: http://code.google.com/p/psutil/issues/detail?id=440
The text was updated successfully, but these errors were encountered: