Commit b68f8105 authored by Kento HASEGAWA's avatar Kento HASEGAWA
Browse files

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

parent b33ac986
Loading
Loading
Loading
Loading
+19 −60
Changes for comm/client/main.py: 19 added lines, 60 removed lines.
Original line number Diff line number Diff line
@@ -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)
	
        pynq_thread.start()
        boardstr = BoardStr.conv_boardstr(qstr.split('\n'), 'random', int(qseed))
        option = {"name": qname, "host": args['host'], "client": client_baseurl} 

        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"