From bc20e162e6b2294d043f160a22bc8eaafafd7e4e Mon Sep 17 00:00:00 2001 From: tanacchi Date: Wed, 27 Nov 2019 15:24:27 +0900 Subject: [PATCH] Upgrade SubprocessWrapper with registering func to atexit --- route_guidance_ros/scripts/utilities.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/route_guidance_ros/scripts/utilities.py b/route_guidance_ros/scripts/utilities.py index 8ff7edbd..0802b5e4 100644 --- a/route_guidance_ros/scripts/utilities.py +++ b/route_guidance_ros/scripts/utilities.py @@ -1,13 +1,22 @@ import time +import subprocess from subprocess import Popen +import atexit +def register_process_killer(process): + def kill_subprocess(pid): + subprocess.run(["kill", "-SIGKILL", str(pid)]) + print("process {} was killed.".format(pid)) + atexit.register(kill_subprocess, process.pid) class SubprocessWrapper(object): def __init__(self, command): self.process = Popen(command) + register_process_killer(self.process) - def __del__(self): - self.process.kill() + # It is not guaranteed that __del__() methods are called when the interpreter exits. + # def __del__(self): + # self.process.kill() class ComponentWrapper(SubprocessWrapper): -- GitLab