feat: 强力关闭场景
This commit is contained in:
parent
ab024afb30
commit
614b13e58c
@ -143,7 +143,6 @@ class GazeboSimHttpServer(HttpRPCServer):
|
||||
# 关闭现有场景
|
||||
if self.scene_mgr.running_scene:
|
||||
self.shut_scene()
|
||||
time.sleep(25)
|
||||
# 启动场景
|
||||
self.scene_mgr.start_scene(id)
|
||||
self.model_mgr = ModelManager()
|
||||
@ -163,11 +162,8 @@ class GazeboSimHttpServer(HttpRPCServer):
|
||||
try:
|
||||
if not self.scene_mgr.running_scene:
|
||||
raise RuntimeError('No scene is running')
|
||||
|
||||
def shutt():
|
||||
self.scene_mgr.shut_scene_truly()
|
||||
|
||||
threading.Thread(target=shutt, daemon=True).start()
|
||||
self.scene_mgr.shut_scene()
|
||||
time.sleep(5)
|
||||
self.model_mgr = None
|
||||
except Exception as e:
|
||||
ret['code'] = 400
|
||||
|
||||
@ -89,21 +89,9 @@ class SceneManager:
|
||||
self.pid_file = None
|
||||
self.sim_process.terminate()
|
||||
print(f'killed launch PID: {self.sim_process.pid}')
|
||||
# 杀死gazebo和rviz
|
||||
subprocess.Popen(['pkill', '-15', '-f', 'gzserver'])
|
||||
subprocess.Popen(['pkill', '-15', '-f', 'gzclient'])
|
||||
subprocess.Popen(['pkill', '-15', '-f', 'rviz'])
|
||||
self.sim_process = None
|
||||
self.kill_all_processes()
|
||||
self.running_scene = None
|
||||
|
||||
def shut_scene_truly(self):
|
||||
id = self.running_scene
|
||||
self.shut_scene()
|
||||
time.sleep(2)
|
||||
self.start_scene(id)
|
||||
time.sleep(15)
|
||||
self.shut_scene()
|
||||
|
||||
def run_script(self, name):
|
||||
if not self.running_scene:
|
||||
raise RuntimeError('no scene running')
|
||||
@ -113,6 +101,28 @@ class SceneManager:
|
||||
raise ValueError(f'script {name} not found')
|
||||
subprocess.Popen(['bash', script_path])
|
||||
|
||||
def kill_all_processes(self):
|
||||
try:
|
||||
# 获取所有进程
|
||||
output = subprocess.check_output(["ps", "aux"]).decode()
|
||||
|
||||
# 过滤出目标进程
|
||||
target_pids = []
|
||||
for line in output.splitlines():
|
||||
# 排除包含"gazebo_world_manager"的进程
|
||||
if any(x in line for x in ["ros", "rviz", "gazebo"]) and "grep" not in line and "gazebo_world_manager" not in line:
|
||||
target_pids.append(line.split()[1]) # 提取PID
|
||||
|
||||
if target_pids:
|
||||
# 批量kill
|
||||
subprocess.run(["kill", "-9"] + target_pids)
|
||||
print(f"Killed PIDs: {', '.join(target_pids)}")
|
||||
else:
|
||||
print("No matching processes running.")
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
mgr = SceneManager()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user