Beremiz_service.py
changeset 1994 1fdc32be71b8
parent 1988 19ca02e8074f
child 1997 d9e8fb47340f
--- a/Beremiz_service.py	Tue Apr 17 11:19:18 2018 +0200
+++ b/Beremiz_service.py	Thu Apr 19 12:22:40 2018 +0200
@@ -30,9 +30,10 @@
 import sys
 import getopt
 import threading
-from threading import Thread, currentThread, Semaphore
+from threading import Thread, currentThread, Semaphore, Lock
 import traceback
 import __builtin__
+import Pyro
 import Pyro.core as pyro
 
 from runtime import PLCObject, ServicePublisher, MainWorker
@@ -399,6 +400,7 @@
     return res
 
 
+
 class Server(object):
     def __init__(self, servicename, ip_addr, port,
                  workdir, argv,
@@ -436,8 +438,9 @@
         sys.stdout.flush()
 
 
-    def PyroLoop(self):
+    def PyroLoop(self, when_ready):
         while self.continueloop:
+            Pyro.config.PYRO_MULTITHREADED = 0
             pyro.initServer()
             self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port)
 
@@ -452,6 +455,7 @@
                 self.servicepublisher = ServicePublisher.ServicePublisher()
                 self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port)
 
+            when_ready()
             self.daemon.requestLoop()
             self.daemon.sock.close()
 
@@ -631,12 +635,20 @@
         except Exception:
             LogMessageAndException(_("WAMP client startup failed. "))
 
-pyro_thread = Thread(target=pyroserver.PyroLoop)
+pyro_thread_started = Lock()
+pyro_thread_started.acquire()
+pyro_thread = Thread(target=pyroserver.PyroLoop,
+                     kwargs=dict(when_ready=pyro_thread_started.release))
 pyro_thread.start()
 
+# Wait for pyro thread to be effective
+pyro_thread_started.acquire()
+
 pyroserver.PrintServerInfo()
 
 if havetwisted or havewx:
+    ui_thread_started = Lock()
+    ui_thread_started.acquire()
     if havetwisted:
         # reactor._installSignalHandlersAgain()
         def ui_thread_target():
@@ -649,6 +661,16 @@
     ui_thread = Thread(target = ui_thread_target)
     ui_thread.start()
 
+    # This order ui loop to unblock main thread when ready.
+    if havetwisted:
+        reactor.callLater(0,ui_thread_started.release)
+    else :
+        wx.CallAfter(ui_thread_started.release)
+
+    # Wait for ui thread to be effective
+    ui_thread_started.acquire()
+    print("UI thread started successfully.")
+
 try:
     MainWorker.runloop(pyroserver.AutoLoad)
 except KeyboardInterrupt: