diff --git a/comm/client/main.py b/comm/client/main.py index d44b3254878421ea5c49c83e1c6c9810121af462..022c5a3b29b80962ebfe255908caa3816fe8d646 100644 --- a/comm/client/main.py +++ b/comm/client/main.py @@ -17,65 +17,17 @@ from urllib.parse import urlparse sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../solver') import BoardStr -import pynqrouter +import adc2018solver as pynqrouter app = Flask(__name__) args = {} -pynq_thread = None +# pynq_thread = None client_baseurl = "" -# Reference: https://teratail.com/questions/52593 -class StoppableThread(threading.Thread): - def __init__(self, target, qname, qstr, qseed): - super(StoppableThread, self).__init__(target=target) - self._th_stop = threading.Event() - self._qname = qname - self._qstr = qstr - self._qseed = int(qseed) - self._answer = None - - def run(self): - global client_baseurl - global pynq_thread - global args - - # Main funciton (the solver should be placed here) - boardstr = BoardStr.conv_boardstr(self._qstr.split('\n'), 'random', self._qseed) - result = pynqrouter.solve(boardstr, self._qseed) - if result['solved']: - answer = result['solution'] - else: - answer = None - pynq_thread = None - return - - res = { - "client": client_baseurl, - "qname": self._qname, - "answer": answer, - "cputime": result['elapsed'] - } - self._answer = answer - r = requests.post("http://{}/post".format(args["host"]), data=res) - - if args["verbose"]: - print(res) - - pynq_thread = None - - def stop(self): - self._th_stop.set() - - def stopped(self): - return self._th_stop.isSet() - - def get_answer(self): - return self._answer - @app.route('/start', methods=["POST"]) def start(): - global pynq_thread + # global pynq_thread global args global client_baseurl @@ -84,13 +36,19 @@ def start(): if args["verbose"]: print(request.form) - if pynq_thread is None: + if pynqrouter.solver_thread is not None: + pynqrouter.stop_solver() + + if pynqrouter.solver_thread is None: qstr = request.form["question"] qname = request.form["qname"] qseed = request.form["qseed"] - pynq_thread = StoppableThread(target=StoppableThread, qname=qname, qstr=qstr, qseed=qseed) + + boardstr = BoardStr.conv_boardstr(qstr.split('\n'), 'random', int(qseed)) + option = {"name": qname, "host": args['host'], "client": client_baseurl} - pynq_thread.start() + pynqrouter.start_solver(boardstr, qseed, option) + # pynq_thread = pynqrouter.start_solver(boardstr, qseed, option) # 実行だけ開始する ans["status"] = "Processing" @@ -106,26 +64,27 @@ def start(): @app.route('/stop') def stop(): - global pynq_thread + # global pynq_thread - if pynq_thread is None: + if pynqrouter.solver_thread is None: ans = {"status": "No threads"} else: - pynq_thread.stop() + # pynq_thread.stop() + pynqrouter.stop_solver() ans = {"status": "Stopped"} - pynq_thread = None + # pynq_thread = None return json.dumps(ans) @app.route("/status") def status(): - global pynq_thread + # global pynq_thread res_mes = "" - if pynq_thread is None: + if pynqrouter.solver_thread is None: res_mes = "Ready" else: res_mes = "Working"