Skip to content

Commit

Permalink
reverted dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Pimpalkhare authored and Nikhil Pimpalkhare committed Oct 23, 2020
1 parent 37f0526 commit cb9f945
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions medleysolver/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,47 @@
import csv

def run_problem(solver, invocation, problem, timeout):
instance = problem.split("/")[-1]
directory = problem[:-len(instance)]

# pass the problem to the command
command = "%s %s" %(invocation, problem)
# get start time
start = datetime.datetime.now().timestamp()
# run command
process = subprocess.Popen(
command,
shell = True,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
preexec_fn = os.setsid
)
# wait for it to complete
try:
with open(directory+"/"+solver+".csv") as csvfile:
results = list(csv.reader(csvfile))
results = list(filter(lambda s: s[0] == problem, results))
assert(len(results) == 1)
output = results[0][4]
output = output2result(problem, output)
elapsed = float(results[0][3])

if elapsed >= timeout:
output = TIMEOUT_RESULT % timeout
elapsed = timeout

result = Result(
problem = problem.split("/", 2)[-1],
result = output,
elapsed = elapsed
)
except:
result = Result(
problem = problem.split("/", 2)[-1],
result = ERROR_RESULT,
elapsed = 0.1 # penalty: simulating time it would have taken to run and fail
)
process.wait(timeout=timeout)
# if it times out ...
except subprocess.TimeoutExpired:
# kill it
# print('TIMED OUT:', repr(command), '... killing', process.pid, file=sys.stderr)
try:
os.killpg(os.getpgid(process.pid), signal.SIGINT)
except:
pass
# set timeout result
elapsed = timeout
output = TIMEOUT_RESULT % timeout
# if it completes in time ...
else:
# measure run time
end = datetime.datetime.now().timestamp()
elapsed = end - start
# get result
stdout = process.stdout.read().decode("utf-8", "ignore")
stderr = process.stderr.read().decode("utf-8", "ignore")
output = output2result(problem, stdout + stderr)
# make result
result = Result(
problem = problem.split("/", 2)[-1],
result = output,
elapsed = elapsed
)
return result

def output2result(problem, output):
Expand Down

0 comments on commit cb9f945

Please sign in to comment.