svghmi/svghmi.py
changeset 3647 7c427418396f
parent 3499 72ee59ff959c
child 3657 e0d6f5f0dcc2
child 3662 364f2409eeda
--- a/svghmi/svghmi.py	Wed Oct 12 11:59:47 2022 +0200
+++ b/svghmi/svghmi.py	Tue Oct 18 11:09:40 2022 +0200
@@ -636,19 +636,29 @@
              svghmi_cmds[thing] = (
                 "Popen(" +
                 repr(shlex.split(given_command.format(**svghmi_options))) +
-                ")") if given_command else "pass # no command given"
+                ")") if given_command else "None # no command given"
 
         runtimefile_path = os.path.join(buildpath, "runtime_%s_svghmi_.py" % location_str)
         runtimefile = open(runtimefile_path, 'w')
         runtimefile.write("""
-# TODO : multiple watchdog (one for each svghmi instance)
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# generated by beremiz/svghmi/svghmi.py
+
+browser_proc = None
+
 def svghmi_{location}_watchdog_trigger():
-    {svghmi_cmds[Watchdog]}
+    global browser_proc
+    restart_proc = {svghmi_cmds[Watchdog]}
+    waitpid_timeout(restart_proc, "SVGHMI watchdog triggered command")
+    waitpid_timeout(browser_proc, "SVGHMI browser process")
+    browser_proc = None
 
 max_svghmi_sessions = {maxConnections_total}
 
 def _runtime_{location}_svghmi_start():
-    global svghmi_watchdog, svghmi_servers
+    global svghmi_watchdog, svghmi_servers, browser_proc
 
     srv = svghmi_servers.get("{interface}:{port}", None)
     if srv is not None:
@@ -673,7 +683,7 @@
 
     path_list.append("{path}")
 
-    {svghmi_cmds[Start]}
+    browser_proc = {svghmi_cmds[Start]}
 
     if {enable_watchdog}:
         if svghmi_watchdog is None:
@@ -686,7 +696,7 @@
 
 
 def _runtime_{location}_svghmi_stop():
-    global svghmi_watchdog, svghmi_servers
+    global svghmi_watchdog, svghmi_servers, browser_proc
 
     if svghmi_watchdog is not None:
         svghmi_watchdog.cancel()
@@ -702,7 +712,10 @@
         svghmi_listener.stopListening()
         svghmi_servers.pop("{interface}:{port}")
 
-    {svghmi_cmds[Stop]}
+    stop_proc = {svghmi_cmds[Stop]}
+    waitpid_timeout(stop_proc, "SVGHMI stop command")
+    waitpid_timeout(browser_proc, "SVGHMI browser process")
+    browser_proc = None
 
         """.format(location=location_str,
                    xhtml=target_fname,