Browse Source

Fix service operations

Yentl Van Tendeloo 4 years ago
parent
commit
82643154d0
4 changed files with 46 additions and 47 deletions
  1. 12 10
      scripts/run_local_modelverse.py
  2. 34 31
      wrappers/modelverse.py
  3. 0 3
      wrappers/test_service.py
  4. 0 3
      wrappers/test_service_client.py

+ 12 - 10
scripts/run_local_modelverse.py

@@ -9,17 +9,19 @@ else:
     port = sys.argv[1]
 
 # Start up the HUTN compilation service already
-hutn = subprocess.Popen([sys.executable, "scripts/HUTN_service.py", "127.0.0.1:%s" % port])
+try:
+    hutn = subprocess.Popen([sys.executable, "scripts/HUTN_service.py", "127.0.0.1:%s" % port])
 
-os.chdir("hybrid_server")
-subprocess.check_call([sys.executable, "-m", "sccd.compiler.sccdc", "-p", "threads", "server.xml"])
+    os.chdir("hybrid_server")
+    subprocess.check_call([sys.executable, "-m", "sccd.compiler.sccdc", "-p", "threads", "server.xml"])
 
-program_to_execute = [sys.executable, "run_mvk_server.py", port]
+    program_to_execute = [sys.executable, "run_mvk_server.py", port]
 
-# Alternative execution modes
-#program_to_execute = [sys.executable, "-m", "cProfile", "-s", "tottime", "run_mvk_server.py", port]
-#program_to_execute = [sys.executable, "run_mvk_server.py", port, "--kernel=interpreter"]
-#program_to_execute = [sys.executable, "run_mvk_server.py", port, "--kernel=fast-jit"]
+    # Alternative execution modes
+    #program_to_execute = [sys.executable, "-m", "cProfile", "-s", "tottime", "run_mvk_server.py", port]
+    #program_to_execute = [sys.executable, "run_mvk_server.py", port, "--kernel=interpreter"]
+    #program_to_execute = [sys.executable, "run_mvk_server.py", port, "--kernel=fast-jit"]
 
-subprocess.call(program_to_execute)
-hutn.terminate()
+    subprocess.call(program_to_execute)
+finally:
+    hutn.terminate()

+ 34 - 31
wrappers/modelverse.py

@@ -71,19 +71,19 @@ outputs = {}
 ctrl_input = None
 ctrl_output = None
 
-def _output_thread(controller, outputs, taskname):
+def _output_thread(controller, outp, task):
     req_out = controller.addOutputListener("request_out")
     my_id = str(uuid.uuid4())
 
     try:
         while 1:
-            controller.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "get_output", "taskname": taskname}), my_id]))
-
+            controller.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "get_output", "taskname": task}), my_id]))
             event = req_out.fetch(-1)
+
             if event.parameters[1] == my_id:
-                outputs[taskname].append(json.loads(event.parameters[0]))
+                outp[task].append(json.loads(event.parameters[0]))
     except:
-        pass
+        raise
 
 def _exec_on_statechart(statechart):
     def _exec_sc(controller, inport, outport):
@@ -164,17 +164,17 @@ def _goto_mode(new_mode, model_name=None):
         # Go to a mode that we have no automatic transfer to: raise exception
         raise InvalidMode("Required mode: %s, current mode: %s" % (new_mode, mode))
 
-def _input(value, port=None):
+def _input(value, task=None):
     # Ugly json encoding of primitives
     #print("[IN] %s" % value)
-    if port is None:
-        port = taskname
+    if task is None:
+        task = taskname
     if isinstance(value, type([])):
         value = json.dumps(value)
-        ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": port, "data": value}), None]))
+        ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": task, "data": value}), None]))
     else:
         value = json.dumps(value)
-        ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": port, "value": value}), None]))
+        ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": task, "value": value}), None]))
 
 def _input_raw(value, taskname):
     # Ugly json encoding of primitives
@@ -214,28 +214,28 @@ def _compile_model(code):
 
     return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M")
 
-def _output(expected=None, port=None):
-    if port is None:
-        port = taskname
+def _output(expected=None, task=None):
+    if task is None:
+        task = taskname
 
     try:
-        while len(outputs[port]) < 2:
+        while len(outputs[task]) < 2:
             time.sleep(0.02)
 
-        del outputs[port][0]
+        del outputs[task][0]
         #print("[OUT] %s" % outputs[0])
     except:
         raise UnknownError()
 
-    if expected is not None and _last_output(port) != expected:
-        raise InterfaceMismatch(_last_output(port), expected)
-    return _last_output(port)
+    if expected is not None and _last_output(task) != expected:
+        raise InterfaceMismatch(_last_output(task), expected)
+    return _last_output(task)
 
-def _last_output(port=None):
-    if port is None:
-        port = taskname
+def _last_output(task=None):
+    if task is None:
+        task = taskname
 
-    return outputs[port][0]
+    return outputs[task][0]
 
 # Raise common exceptions
 def _handle_output(requested=None, split=False):
@@ -353,7 +353,7 @@ def init(address_param="127.0.0.1:8001", timeout=20.0):
 
     global mode
     start_time = time.time()
-    task = random.random()
+    task = str(random.random())
 
     _input_raw('"%s"' % task, "task_manager")
 
@@ -991,10 +991,12 @@ def service_register(name, function):
 
     # Now we are in service-mode
     mode = MODE_SERVICE
-    port = _handle_output("Success: ", split=True)[0]
+    task = _handle_output("Success: ", split=True)[0]
 
     # Process events in the background!
-    threading.Thread(target=service_process, args=[port]).start()
+    thrd = threading.Thread(target=service_process, args=[task])
+    thrd.daemon = True
+    thrd.start()
 
 def service_stop():
     """Stop the currently executing process."""
@@ -1005,18 +1007,19 @@ def service_stop():
     global mode
     mode = MODE_MODELLING
 
-def service_get(port):
-    """Get the values on the specified port."""
+def service_get(task):
+    """Get the values on the specified task."""
     _goto_mode(MODE_SERVICE)
 
-    return _output(port=port)
+    val = _output(task=task)
+    return val
 
-def service_set(port, value):
-    """Set a value on a specified port."""
+def service_set(task, value):
+    """Set a value on a specified task."""
     _check_type_list(value)
     _goto_mode(MODE_SERVICE)
 
-    _input(value, port=port)
+    _input(value, task=task)
 
 def user_password(user, password):
     """Change a user's password."""

+ 0 - 3
wrappers/test_service.py

@@ -13,12 +13,9 @@ def fibonacci_service(port):
     service_set(port, fibonacci(service_get(port)))
     
 service_register("fibonacci", fibonacci_service)
-print("Registered")
 
 while raw_input() != "STOP":
     # Stay active, as we shouldn't exit while the service is running!
     pass
-print("Stopping")
 
 service_stop()
-print("Service halted")

+ 0 - 3
wrappers/test_service_client.py

@@ -2,8 +2,5 @@ from modelverse import *
 
 init()
 login("admin", "admin")
-print("Upload AL")
 transformation_add_AL({}, {}, "test_services", open("integration/code/simple_client.alc", 'r').read())
-print("DONE")
 transformation_execute_AL("test_services", {}, {})
-print("Executed")