main.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 import rospy
4 import signal
5 import subprocess
6 import threading
7 from subprocess import PIPE
8 import os
9 import psutil
10 
11 from gstreamer_launcher.srv import *
12 
13 exiting_thread = None
14 
15 def exiting():
16  rospy.sleep(0.2)
17  rospy.signal_shutdown("finish")
18 
19 
20 
21 def launcher(req):
22  global exiting_thread
23 
24  if req.message_type == "LAUNCH":
25  proc = subprocess.Popen(req.command, shell=True, restore_signals=True, stdout=PIPE, stderr=PIPE)
26  try:
27  res = proc.communicate(timeout=0.1)
28  except subprocess.TimeoutExpired:
29  return GStreamerLauncherResponse(True, proc.pid)
30 
31  return GStreamerLauncherResponse(False, -1)
32  elif req.message_type == "EXIT":
33  try:
34  os.killpg(os.getpgid(req.pid), signal.SIGTERM)
35  except ProcessLookupError:
36  return GStreamerLauncherResponse(False, -1)
37 
38  return GStreamerLauncherResponse(True, 0)
39  elif req.message_type == "SYSTEM_EXIT":
40  # メッセージだけ返したらexitする
41  exiting_thread = threading.Thread(target=exiting)
42  exiting_thread.start()
43  return GStreamerLauncherResponse(True, 0)
44 
45  return GStreamerLauncherResponse(False, -1)
46 
47 
49  s = rospy.Service('gst_launch', GStreamerLauncher, launcher)
50  rospy.spin()
51 
52 
53 def sigterm_handler(signal, frame):
54  # SYSTEM_EXITを受け取らない限り何があっても落とさない
55  # 単独で起動することはせず、roslaunch内でclientと一緒に起動すべき
56  pass
57 
58 
59 def main():
60  global exiting_thread
62  if not exiting_thread is None:
63  exiting_thread.join()
64 
65 
66 if __name__ == "__main__":
67  rospy.init_node('gstreamer_launcher', log_level=rospy.INFO)
68  signal.signal(signal.SIGTERM, sigterm_handler)
69  signal.signal(signal.SIGINT, sigterm_handler)
70  main()
def gst_launch_server()
Definition: main.py:48
def launcher(req)
Definition: main.py:21
def sigterm_handler(signal, frame)
Definition: main.py:53
def main()
Definition: main.py:59
def exiting()
Definition: main.py:15
Definition: main.py:1


skyway
Author(s): Toshiya Nakakura
autogenerated on Sat Apr 15 2023 02:08:21