Commit 72e19f13 authored by Kento HASEGAWA's avatar Kento HASEGAWA

Add a queue to the solver worker

parent fb0ef6af
......@@ -6,6 +6,8 @@ import sys
import time
import threading
from collections import OrderedDict
class Solver(object):
def __init__(self, config):
......@@ -15,7 +17,13 @@ class Solver(object):
self.address = config['address']
self.name = config['name']
self.solver = importlib.import_module(f"solvers.{config['solver']}")
self.thread = None
self.queue = OrderedDict()
self.status = 'Ready'
# self.thread = None
self.thread = threading.Thread(name='solver', target=self.solver_thread, daemon=True)
self.thread.start()
def __del__(self):
self.stop_solver()
......@@ -23,6 +31,18 @@ class Solver(object):
def __repr__(self):
return "Solver"
def solver_thread(self):
while True:
if len(self.queue) > 0:
print("I: Solver started")
self.status = 'running'
_, params = self.queue.popitem(last=False)
self.solve(params)
else:
self.status = 'ready'
time.sleep(0.5)
def solve(self, params):
start_time = time.time()
......@@ -35,7 +55,7 @@ class Solver(object):
solution['elapsed_time'] = elapsed_time
self.submit_solution(params, solution)
self.thread = None
# self.thread = None
return True
def post(self, path, data):
......@@ -57,14 +77,18 @@ class Solver(object):
def start_solver(self, params):
if self.thread is None:
print("I: Solver started")
# self.thread = StoppableThread(target=self.solve, args=(params, ))
self.thread = threading.Thread(name='solver', target=self.solve, args=(params, ), daemon=True)
self.thread.start()
return {'status': 'started'}
else:
return {'status': 'busy'}
# if self.thread is None:
# print("I: Solver started")
# self.thread = threading.Thread(name='solver', target=self.solve, args=(params, ), daemon=True)
# self.thread.start()
# return {'status': 'started'}
# else:
# return {'status': 'busy'}
print("I: Problem queued")
_id = params['request_id']
self.queue[_id] = params
return {'status': 'queued'}
def stop_solver(self):
......@@ -80,5 +104,7 @@ class Solver(object):
return self.start_solver(params)
elif cmd == 'stop':
return self.stop_solver()
elif cmd == 'status':
return self.status
else:
return None
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