svghmi/svghmi_server.py
changeset 3647 7c427418396f
parent 3346 3b920d1fe81b
child 3661 62860665fa94
--- a/svghmi/svghmi_server.py	Wed Oct 12 11:59:47 2022 +0200
+++ b/svghmi/svghmi_server.py	Tue Oct 18 11:09:40 2022 +0200
@@ -8,6 +8,7 @@
 from __future__ import absolute_import
 import errno
 from threading import RLock, Timer
+import os, time
 
 try:
     from runtime.spawn_subprocess import Popen
@@ -23,6 +24,9 @@
 from autobahn.websocket.protocol import WebSocketProtocol
 from autobahn.twisted.resource import  WebSocketResource
 
+from runtime.loglevels import LogLevelsDict
+from runtime import GetPLCObjectSingleton
+
 max_svghmi_sessions = None
 svghmi_watchdog = None
 
@@ -299,3 +303,21 @@
     render_HEAD = render_GET
 
 
+def waitpid_timeout(proc, helpstr="", timeout = 3):
+    if proc is None:
+        return
+    def waitpid_timeout_loop(pid=proc.pid, timeout = timeout):
+        try:
+            while os.waitpid(pid,os.WNOHANG) == (0,0):
+                time.sleep(1)
+                timeout = timeout - 1
+                if not timeout:
+                    GetPLCObjectSingleton().LogMessage(
+                        LogLevelsDict["WARNING"], 
+                        "Timeout waiting for {} PID: {}".format(helpstr, str(pid)))
+                    break
+        except OSError:
+            # workaround exception "OSError: [Errno 10] No child processes"
+            pass
+    Thread(target=waitpid_timeout_loop, name="Zombie hunter").start()
+