Commit b68f8105 authored by Kento HASEGAWA's avatar Kento HASEGAWA

Make it possible to stop a running solver (#7)

parent b33ac986
...@@ -17,65 +17,17 @@ from urllib.parse import urlparse ...@@ -17,65 +17,17 @@ from urllib.parse import urlparse
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../solver') sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../solver')
import BoardStr import BoardStr
import pynqrouter import adc2018solver as pynqrouter
app = Flask(__name__) app = Flask(__name__)
args = {} args = {}
pynq_thread = None # pynq_thread = None
client_baseurl = "" 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"]) @app.route('/start', methods=["POST"])
def start(): def start():
global pynq_thread # global pynq_thread
global args global args
global client_baseurl global client_baseurl
...@@ -84,13 +36,19 @@ def start(): ...@@ -84,13 +36,19 @@ def start():
if args["verbose"]: if args["verbose"]:
print(request.form) 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"] qstr = request.form["question"]
qname = request.form["qname"] qname = request.form["qname"]
qseed = request.form["qseed"] 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" ans["status"] = "Processing"
...@@ -106,26 +64,27 @@ def start(): ...@@ -106,26 +64,27 @@ def start():
@app.route('/stop') @app.route('/stop')
def stop(): def stop():
global pynq_thread # global pynq_thread
if pynq_thread is None: if pynqrouter.solver_thread is None:
ans = {"status": "No threads"} ans = {"status": "No threads"}
else: else:
pynq_thread.stop() # pynq_thread.stop()
pynqrouter.stop_solver()
ans = {"status": "Stopped"} ans = {"status": "Stopped"}
pynq_thread = None # pynq_thread = None
return json.dumps(ans) return json.dumps(ans)
@app.route("/status") @app.route("/status")
def status(): def status():
global pynq_thread # global pynq_thread
res_mes = "" res_mes = ""
if pynq_thread is None: if pynqrouter.solver_thread is None:
res_mes = "Ready" res_mes = "Ready"
else: else:
res_mes = "Working" res_mes = "Working"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment