Skip to content

Commit

Permalink
Implement delayed termination of the client on SIGINT (e.g. ctrl-c)
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
kiudee committed Feb 2, 2020
1 parent 6e459e0 commit 6780987
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
16 changes: 15 additions & 1 deletion tune/db_workers/tuning_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import re
import signal
import subprocess
import sys
import numpy as np
Expand All @@ -21,6 +22,8 @@ def __init__(self, dbconfig_path, **kwargs):
self.logger = logging.getLogger("TuningClient")
self.lc0_benchmark = None
self.sf_benchmark = None
signal.signal(signal.SIGINT, self.interrupt_handler)
self.interrupt_pressed = False
if os.path.isfile(dbconfig_path):
with open(dbconfig_path, "r") as config_file:
config = config_file.read().replace("\n", "")
Expand All @@ -29,6 +32,15 @@ def __init__(self, dbconfig_path, **kwargs):
else:
raise ValueError("No config file found at provided path")

def interrupt_handler(self, sig, frame):
if self.interrupt_pressed:
self.logger.info("Shutting down immediately.")
sys.exit(0)
self.interrupt_pressed = True
self.logger.info(
"Signal received. Shutting down after next match.\nPress a second time to terminate immediately."
)

def run_experiment(self, time_control, cutechess_options):
try:
os.remove("out.pgn")
Expand Down Expand Up @@ -152,8 +164,10 @@ def pick_job(self, jobs, mix=0.25):

def run(self):
while True:
if self.interrupt_pressed:
self.logger.info('Shutting down after receiving shutdown signal.')
sys.exit(0)
# 1. Check db for new job

with psycopg2.connect(**self.connect_params) as conn:
with conn.cursor(cursor_factory=DictCursor) as curs:
job_string = """
Expand Down
13 changes: 1 addition & 12 deletions tune/db_workers/tuning_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
"""The server worker, which reads results and schedules new jobs.
Usage:
tuning_server.py (run | deactivate | reactivate) [options] <experiment_file> <dbconfig>
tuning_server.py -h | --help
Options:
-h --help Show this screen.
-v --verbose Set log level to debug.
--logfile=<path> Path to where the log is output to
"""
"""The server worker, which reads results and schedules new jobs."""
import joblib
import json
import logging
Expand Down

0 comments on commit 6780987

Please sign in to comment.