skip lock if idle

This commit is contained in:
Sander Land committed 2021-01-17 12:20:16 +01:00
1 parent 6cdd1154dc
commit d9c94b9510
3 files changed
+69 -61

No files matched your search

+46 -41
View File
@@ -52,7 +52,7 @@ class KataGoEngine:
self.write_stdin_thread = None
self.shell = False
self.write_queue = queue.Queue()
self.thread_lock = threading.Lock()
exe = config.get("katago", "").strip()
if config.get("altcommand", ""):
self.command = config["altcommand"]
@@ -92,39 +92,43 @@ class KataGoEngine:
self.start()
def start(self):
self.write_queue = queue.Queue()
try:
self.katrain.log(f"Starting KataGo with {self.command}", OUTPUT_DEBUG)
startupinfo = None
if hasattr(subprocess, "STARTUPINFO"):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # stop command box popups on win/pyinstaller
self.katago_process = subprocess.Popen(
self.command,
startupinfo=startupinfo,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=self.shell,
)
except (FileNotFoundError, PermissionError, OSError) as e:
self.katrain.log(
i18n._("Starting Kata failed").format(command=self.command, error=e),
OUTPUT_ERROR,
)
return # don't start
self.analysis_thread = threading.Thread(target=self._analysis_read_thread, daemon=True)
self.stderr_thread = threading.Thread(target=self._read_stderr_thread, daemon=True)
self.write_stdin_thread = threading.Thread(target=self._write_stdin_thread, daemon=True)
self.analysis_thread.start()
self.stderr_thread.start()
self.write_stdin_thread.start()
with self.thread_lock:
self.write_queue = queue.Queue()
try:
self.katrain.log(f"Starting KataGo with {self.command}", OUTPUT_DEBUG)
startupinfo = None
if hasattr(subprocess, "STARTUPINFO"):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # stop command box popups on win/pyinstaller
self.katago_process = subprocess.Popen(
self.command,
startupinfo=startupinfo,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=self.shell,
)
except (FileNotFoundError, PermissionError, OSError) as e:
self.katrain.log(
i18n._("Starting Kata failed").format(command=self.command, error=e),
OUTPUT_ERROR,
)
return # don't start
self.analysis_thread = threading.Thread(target=self._analysis_read_thread, daemon=True)
self.stderr_thread = threading.Thread(target=self._read_stderr_thread, daemon=True)
self.write_stdin_thread = threading.Thread(target=self._write_stdin_thread, daemon=True)
self.analysis_thread.start()
self.stderr_thread.start()
self.write_stdin_thread.start()
def on_new_game(self):
self.base_priority += 1
for query_id in list(self.queries.keys()):
self.terminate_query(query_id)
self.queries = {}
if not self.is_idle():
with self.thread_lock:
for query_id in list(self.queries.keys()):
self.terminate_query(query_id)
self.queries = {}
self.write_queue = queue.Queue()
def restart(self):
self.queries = {}
@@ -248,16 +252,17 @@ class KataGoEngine:
query, callback, error_callback, next_move = self.write_queue.get(block=True, timeout=0.1)
except queue.Empty:
continue
if "id" not in query:
self.query_counter += 1
query["id"] = f"QUERY:{str(self.query_counter)}"
self.queries[query["id"]] = (callback, error_callback, time.time(), next_move)
self.katrain.log(f"Sending query {query['id']}: {json.dumps(query)}", OUTPUT_DEBUG)
try:
self.katago_process.stdin.write((json.dumps(query) + "\n").encode())
self.katago_process.stdin.flush()
except OSError as e:
self.check_alive(os_error=str(e), exception_if_dead=False)
with self.thread_lock:
if "id" not in query:
self.query_counter += 1
query["id"] = f"QUERY:{str(self.query_counter)}"
self.queries[query["id"]] = (callback, error_callback, time.time(), next_move)
self.katrain.log(f"Sending query {query['id']}: {json.dumps(query)}", OUTPUT_DEBUG)
try:
self.katago_process.stdin.write((json.dumps(query) + "\n").encode())
self.katago_process.stdin.flush()
except OSError as e:
self.check_alive(os_error=str(e), exception_if_dead=False)
def send_query(self, query, callback, error_callback, next_move=None):
self.write_queue.put((query, callback, error_callback, next_move))
+18 -15
View File
@@ -598,22 +598,25 @@ class ConfigPopup(QuickConfigGui):
savepath = os.path.expanduser(os.path.join(DATA_FOLDER, filename))
savepath_tmp = savepath + ".part"
self.katrain.log(f"Downloading {name} model from {url} to {savepath_tmp}", OUTPUT_INFO)
progress = ProgressLoader(
download_url=url,
path_to_file=savepath_tmp,
downloading_text=f"Downloading {name} model: " + "{}",
label_downloading_text=f"Starting download for {name} model",
download_complete=lambda req, tmp=savepath_tmp, path=savepath, model=name: download_complete(
req, tmp, path, model
Clock.schedule_once(
lambda _dt, _savepath_tmp=savepath_tmp, _url=url: ProgressLoader(
self.download_progress_box,
download_url=url,
path_to_file=savepath_tmp,
downloading_text=f"Downloading {name} model: " + "{}",
label_downloading_text=f"Starting download for {name} model",
download_complete=lambda req, tmp=savepath_tmp, path=savepath, model=name: download_complete(
req, tmp, path, model
),
download_redirected=lambda req, mname=name: self.katrain.log(
f"Download {mname} redirected {req.resp_headers}", OUTPUT_DEBUG
),
download_error=lambda req, error, mname=name: self.katrain.log(
f"Download of {mname} failed or cancelled ({error})", OUTPUT_ERROR
),
),
download_redirected=lambda req, mname=name: self.katrain.log(
f"Download {mname} redirected {req.resp_headers}", OUTPUT_DEBUG
),
download_error=lambda req, error, mname=name: self.katrain.log(
f"Download of {mname} failed or cancelled ({error})", OUTPUT_ERROR
),
)
Clock.schedule_once(lambda _dt, pl=progress: pl.start(self.download_progress_box), 0) # main thread
0,
) # main thread
downloading = True
if not downloading:
Clock.schedule_once(
+5 -5
View File
@@ -59,13 +59,13 @@ class ProgressLoader(BoxLayout):
request = ObjectProperty()
"""UrlRequest object."""
def __init__(self, **kwargs):
def __init__(self, root_instance, **kwargs):
super().__init__(**kwargs)
self.root_instance = None
self.request = None
def start(self, root_instance):
self.root_instance = root_instance
self.request = None
Clock.schedule_once(self.start, 0)
def start(self, _dt):
self.root_instance.add_widget(self)
self.request_download_file(self.download_url, self.path_to_file)
Clock.schedule_once(self.animation_show, 1)