a.update(b)
return a
-def shutdown_process(process):
- i = 0
- while i < 100:
- try:
- os.kill(process.pid, signal.SIGTERM)
- except OSError as e:
- assert e.errno == errno.ESRCH
- return
- if process.status() == psutil.STATUS_ZOMBIE:
- return
+TERM_TIMEOUT = 10 # wait after sending a SIGTERM signal
+KILL_WAIT = 20 # additional wait after sending a SIGKILL signal
- i += 1
- time.sleep(0.1)
+def shutdown_process(process):
+ # send SIGTERM
+ process.terminate()
- while i < 200:
+ try:
+ process.wait(TERM_TIMEOUT)
+ return
+ except psutil.TimeoutExpired:
+ logger.info( "PID {} is not termianated in {} seconds, sending SIGKILL..." %
+ (process.pid, TERM_TIMEOUT))
try:
- os.kill(process.pid, signal.SIGKILL)
- except OSError as e:
- assert e.errno == errno.ESRCH
+ # send SIGKILL
+ process.kill()
+ except psutil.NoSuchProcess:
+ # process exited just befor we tried to kill
return
- if process.status() == psutil.STATUS_ZOMBIE:
- return
-
- i += 1
- time.sleep(0.1)
- assert False, "Failed to shutdown process %d (%s)" % (process.pid, process.name())
+ try:
+ process.wait(KILL_WAIT)
+ except psutil.TimeoutExpired:
+ raise RuntimeError("Failed to shutdown process %d (%s)" % (process.pid, process.name()))
def shutdown_process_with_children(pid):