From bebd25d9018e17339cc22ae990a1063014717128 Mon Sep 17 00:00:00 2001 From: Torsten Hellwig Date: Mon, 5 Aug 2024 20:30:41 +0200 Subject: [PATCH] Don't save ponder task --- engine.py | 16 +++++++--------- game.py | 4 ++-- lichess_game.py | 10 +++++----- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/engine.py b/engine.py index 89479b7..03dcbcd 100644 --- a/engine.py +++ b/engine.py @@ -1,6 +1,6 @@ import os import subprocess -from asyncio import SubprocessTransport, Task, create_task +from asyncio import SubprocessTransport import chess from chess.engine import INFO_ALL, InfoDict, Limit, Opponent, Option, UciProtocol, popen_uci @@ -13,13 +13,11 @@ def __init__(self, transport: SubprocessTransport, engine: UciProtocol, ponder: bool, - opponent: Opponent, - ponder_task: Task | None) -> None: + opponent: Opponent) -> None: self.transport = transport self.engine = engine self.ponder = ponder self.opponent = opponent - self.ponder_task = ponder_task @classmethod async def from_config(cls, @@ -34,7 +32,7 @@ async def from_config(cls, await cls._configure_engine(engine, uci_options) await engine.send_opponent_information(opponent=opponent) - return cls(transport, engine, engine_config.ponder, opponent, None) + return cls(transport, engine, engine_config.ponder, opponent) @classmethod async def test(cls, engine_config: Engine_Config, syzygy_config: Syzygy_Config) -> None: @@ -98,14 +96,14 @@ async def make_move(self, return result.move, result.info - def start_pondering(self, board: chess.Board) -> None: + async def start_pondering(self, board: chess.Board) -> None: if self.ponder: - self.ponder_task = create_task(self.engine.analysis(board)) + await self.engine.analysis(board) - def stop_pondering(self) -> None: + async def stop_pondering(self) -> None: if self.ponder: self.ponder = False - self.ponder_task = create_task(self.engine.analysis(chess.Board(), Limit(time=0.001))) + await self.engine.analysis(chess.Board(), Limit(time=0.001)) async def close(self) -> None: try: diff --git a/game.py b/game.py index 91291e0..774a2a9 100755 --- a/game.py +++ b/game.py @@ -55,7 +55,7 @@ async def run(self) -> None: if self.lichess_game.is_our_turn: await self._make_move() else: - self.lichess_game.start_pondering() + await self.lichess_game.start_pondering() opponent_title = self.info.black_title if self.lichess_game.is_white else self.info.white_title abortion_seconds = 30.0 if opponent_title == 'BOT' else 60.0 @@ -80,7 +80,7 @@ async def run(self) -> None: if self.lichess_game.is_our_turn: await self._make_move() else: - self.lichess_game.start_pondering() + await self.lichess_game.start_pondering() elif event['type'] == 'gameState': self.lichess_game.update(event) diff --git a/lichess_game.py b/lichess_game.py index c2ec2b1..e58cd3c 100644 --- a/lichess_game.py +++ b/lichess_game.py @@ -68,7 +68,7 @@ async def make_move(self) -> Lichess_Move: self.board.push(move_response.move) if not move_response.is_engine_move: - self.engine.start_pondering(self.board) + await self.engine.start_pondering(self.board) print(f'{move_response.public_message} {move_response.private_message}'.strip()) self.last_message = move_response.public_message @@ -120,8 +120,8 @@ def engine_times(self) -> tuple[float, float, float]: return self.white_time, black_time, self.increment - def start_pondering(self) -> None: - self.engine.start_pondering(self.board) + async def start_pondering(self) -> None: + await self.engine.start_pondering(self.board) async def end_game(self) -> None: await self.engine.close() @@ -492,7 +492,7 @@ async def _make_gaviota_move(self) -> Move_Response | None: else: return - self.engine.stop_pondering() + await self.engine.stop_pondering() move = random.choice(best_moves) message = f'Gaviota: {self._format_move(move):14} {egtb_info}' return Move_Response(move, message, is_drawish=offer_draw, is_resignable=resign) @@ -565,7 +565,7 @@ async def _make_syzygy_move(self) -> Move_Response | None: offer_draw = False resign = True - self.engine.stop_pondering() + await self.engine.stop_pondering() move = random.choice(best_moves) message = f'Syzygy: {self._format_move(move):14} {egtb_info}' return Move_Response(move, message, is_drawish=offer_draw, is_resignable=resign)