From 91406d670e0f88db58e45d36452ad8ea34ff9673 Mon Sep 17 00:00:00 2001 From: Jon Hagg Date: Wed, 5 May 2021 14:16:16 -0700 Subject: [PATCH] refactor: make api available outside flask context --- pyreisejl/utility/app.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pyreisejl/utility/app.py b/pyreisejl/utility/app.py index a962149d..8790f0c8 100644 --- a/pyreisejl/utility/app.py +++ b/pyreisejl/utility/app.py @@ -26,11 +26,8 @@ def get_script_path(): return str(path_to_script) -@app.route("/launch/", methods=["POST"]) -def launch_simulation(scenario_id): +def launch_simulation(scenario_id, threads=None, solver=None): cmd_call = ["python3", "-u", get_script_path(), str(scenario_id), "--extract-data"] - threads = request.args.get("threads", None) - solver = request.args.get("solver", None) if threads is not None: cmd_call.extend(["--threads", str(threads)]) @@ -41,12 +38,24 @@ def launch_simulation(scenario_id): proc = Popen(cmd_call, stdout=PIPE, stderr=PIPE, start_new_session=True) entry = SimulationState(scenario_id, proc) state.add(entry) - return jsonify(entry.as_dict()) + return entry.as_dict() + + +def check_progress(): + return state.as_dict() + + +@app.route("/launch/", methods=["POST"]) +def handle_launch(scenario_id): + threads = request.args.get("threads", None) + solver = request.args.get("solver", None) + entry = launch_simulation(scenario_id, threads, solver) + return jsonify(entry) @app.route("/list") def list_ongoing(): - return jsonify(state.as_dict()) + return jsonify(check_progress()) @app.route("/status/")