Jelajahi Sumber

Allow multiple init calls after one another, where each init opens a new task

Yentl Van Tendeloo 8 tahun lalu
induk
melakukan
b12fea4781
1 mengubah file dengan 13 tambahan dan 10 penghapusan
  1. 13 10
      wrappers/modelverse.py

+ 13 - 10
wrappers/modelverse.py

@@ -70,7 +70,7 @@ current_model = None
 registered_metamodels = {}
 outputs = [None]
 
-def _output_thread(outputs):
+def _output_thread(outputs, taskname):
     while taskname is None:
         time.sleep(0.1)
 
@@ -80,10 +80,6 @@ def _output_thread(outputs):
     except:
         pass
 
-thrd = threading.Thread(target=_output_thread, args=[outputs])
-thrd.daemon = True
-thrd.start()
-
 def _exec_on_statechart(statechart):
     def _exec_sc(controller, inport, outport):
         op = controller.addOutputListener(outport)
@@ -94,7 +90,6 @@ def _exec_on_statechart(statechart):
                 del outputs[0]
                 output_event = outputs[0]
 
-                print("Sending MV event: " + str(output_event))
                 if output_event == "Success" or output_event == "Failure":
                     # Is a stop event!
                     controller.addInput(Event("terminate", inport, []))
@@ -106,7 +101,6 @@ def _exec_on_statechart(statechart):
             input_event = op.fetch(0)
             if input_event is not None:
                 # Expand the event and make it HTTP input
-                print("Got SC event: " + str(input_event))
                 _input(input_event.parameters)
                 
             time.sleep(0.01)
@@ -114,6 +108,7 @@ def _exec_on_statechart(statechart):
     thrd = threading.Thread(target=_exec_sc, args=statechart)
     thrd.daemon = True
     thrd.start()
+
     return None
 
 def _get_metamodel(model):
@@ -180,8 +175,6 @@ def _input_raw(value, taskname):
     # Ugly json encoding of primitives
     urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname}))).read()
 
-    #TODO check that this is actually a Modelverse!
-
 def _compile_AL(code):
     # Compile an action language file and send the compiled code
     code_fragments = code.split("\n")
@@ -219,7 +212,8 @@ def _compile_model(code):
 def _output(expected=None):
     try:
         while len(outputs) < 2:
-            time.sleep(0.01)
+            time.sleep(0.5)
+            print("Sleep for output: " + str(outputs))
 
         del outputs[0]
         print("[OUT] %s" % outputs[0])
@@ -319,9 +313,18 @@ def init(address_param="http://127.0.0.1:8001", timeout=20.0):
                 raise ConnectionError(e.reason)
             else:
                 time.sleep(0.1)
+
+    global outputs
     global taskname
+
+    outputs = [None]
     taskname = task
 
+    # This re-assign also diconnects the previous get_output connections to the outputs variable
+    thrd = threading.Thread(target=_output_thread, args=[outputs, task])
+    thrd.daemon = True
+    thrd.start()
+
 def login(username, password):
     """Log in a user, if user doesn't exist, it is created."""
     global mode