From 20a0b986c84ccddd5e71e278e2d0b9be81f60f50 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Wed, 9 Nov 2016 23:38:28 +0800 Subject: webui: Add the foreground simulation task to ConsoleHandler * The original sleep test task "_task()" renamed to "_task_test()" * Also add the new "startServerTaskTest()" to trigger the "_task_test()" NOTE/XXX: The foregrounds simulation requires the configurations, which is currently obtained from "self.websocket.configs", which I think is a HACK. How to better solve this?? --- fg21sim/webui/consolehandler.py | 78 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'fg21sim/webui/consolehandler.py') diff --git a/fg21sim/webui/consolehandler.py b/fg21sim/webui/consolehandler.py index 5c7da00..54d4305 100644 --- a/fg21sim/webui/consolehandler.py +++ b/fg21sim/webui/consolehandler.py @@ -78,6 +78,12 @@ class ConsoleHandler: logger.info("WebSocket: handle message: " + "type: {0}, action: {1}".format(msg_type, msg_action)) if msg_action == "start": + # FIXME/XXX: This task should be asynchronous! + success, error = self._start() + response["success"] = success + if not success: + response["error"] = error + elif msg_action == "start_test": # FIXME/XXX: This task should be asynchronous! success, error = self._start(msg["time"]) response["success"] = success @@ -101,6 +107,35 @@ class ConsoleHandler: logger.debug("WebSocket: response: {0}".format(response)) return response + # FIXME/XXX: + # * How to call this task asynchronously ?? + def _start_test(self, *args, **kwargs): + """ + Start the task by submitting it to the executor + + Returns + ------- + success : bool + Whether success without any errors + error : str + Detail of the error if not succeed + + """ + if self.onetask_only and self.status["running"]: + logger.warning("Task already running, and only one task allowed") + success = False + error = "already running and only one task allowed" + else: + logger.info("Start the task on the executor ...") + self.status["running"] = True + self.status["finished"] = False + # Also push the logging messages to the client + self._add_wsloghandler() + future = self.executor.submit(self._task_test, *args, **kwargs) + self.io_loop.add_future(future, self._task_callback) + success, error = future.result() + return (success, error) + # FIXME/XXX: # * How to call this task asynchronously ?? def _start(self, *args, **kwargs): @@ -170,7 +205,7 @@ class ConsoleHandler: msg_response = json.dumps(response) self.websocket.write_message(msg_response) - def _task(self, *args, **kwargs): + def _task_test(self, *args, **kwargs): """ The task this console to manage. @@ -197,3 +232,44 @@ class ConsoleHandler: time.sleep(1) logger.info("console task: DONE!") return (True, None) + + def _task(self, *args, **kwargs): + """ + The task this console to manage. + Perform the foregrounds simulations. + + Returns + ------- + success : bool + Whether success without any errors + error : str + Detail of the error if not succeed + + NOTE + ---- + The task is synchronous and may be computationally intensive + (i.e., CPU-bound rather than IO/event-bound), therefore, + threads (or processes) are required to make it non-blocking + (i.e., asynchronous). + + Credit: https://stackoverflow.com/a/32164711/4856091 + """ + logger.info("Preparing to start foregrounds simulations ...") + logger.info("Importing modules + Numba JIT, waiting ...") + + from ..foregrounds import Foregrounds + + # FIXME: This is a hack + configs = self.websocket.configs + logger.info("Checking the configurations ...") + configs.check_all() + + fg = Foregrounds(configs) + fg.preprocess() + fg.simulate() + fg.postprocess() + + logger.info("Foregrounds simulations DONE!") + + # NOTE: Should always return a tuple of (success, error) + return (True, None) -- cgit v1.2.2