Skip to content

Commit 0626197

Browse files
add retry logic to evaluate_move helper #13
1 parent 854c063 commit 0626197

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

chmengine/engines/cmhmey2_pool_executor.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Cmhmey Jr.'s Mad Scientist Uncle Who Likes to make clones of Cmhmey Jr."""
22

33
from concurrent.futures import Future, ProcessPoolExecutor, as_completed
4+
from sqlite3 import OperationalError
45
from typing import Dict, Optional, Union
56

67
from chess import Board, Move
@@ -22,16 +23,34 @@ def evaluate_move(board: Board, depth: int = 1, debug: bool = False, cache_dir:
2223
Parameters
2324
----------
2425
board : Board
26+
The chess board state to evaluate.
2527
depth : int
28+
The search depth for the evaluation.
2629
debug : bool
30+
Whether to enable debug output.
2731
cache_dir : str
32+
The cache directory for the engine.
2833
2934
Returns
3035
-------
3136
Pick
37+
The best move and its associated evaluation score.
38+
39+
Raises
40+
------
41+
OperationalError
42+
If the database remains locked after exhausting retries or for unexpected operational errors.
3243
"""
3344
CMHMEngine2.cache_dir = cache_dir
34-
return CMHMEngine2(board=board, depth=depth).pick_move(debug=debug)
45+
try:
46+
return CMHMEngine2(board=board, depth=depth).pick_move(debug=debug)
47+
except OperationalError as error_o:
48+
if "database is locked" in str(error_o):
49+
try:
50+
return evaluate_move(board=board, depth=depth, debug=debug, cache_dir=cache_dir)
51+
except RecursionError:
52+
pass
53+
raise OperationalError('Unexpected Operational Error: {str(error_o)}') from error_o
3554

3655

3756
class CMHMEngine2PoolExecutor:

tests/test_cmhmey2_pool_executor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_debug_output(self):
9696
def test_pick_move(self) -> None:
9797
"""Tests pick_move method."""
9898
start = perf_counter()
99-
pick = self.executor.pick_move(debug=True)
99+
pick = self.executor.pick_move()
100100
duration_first = (perf_counter() - start) / self.executor.engine.board.legal_moves.count()
101101
print(
102102
f"{self.executor.engine.fen()} pick_move call: ({pick[0].uci()},"
@@ -111,7 +111,7 @@ def test_pick_move(self) -> None:
111111
for i, move in enumerate(init_w_moves, 2):
112112
self.executor.engine.board.push(move)
113113
start = perf_counter()
114-
response_pick = self.executor.pick_move(debug=True)
114+
response_pick = self.executor.pick_move()
115115
duration_rep_pick = (perf_counter() - start) / self.executor.engine.board.legal_moves.count()
116116
first_time_pick_times.append(duration_rep_pick)
117117
print(
@@ -120,7 +120,7 @@ def test_pick_move(self) -> None:
120120
)
121121
self.executor.engine.board.pop()
122122
start = perf_counter()
123-
new_pick = self.executor.pick_move(debug=True)
123+
new_pick = self.executor.pick_move()
124124
new_duration = (perf_counter() - start) / self.executor.engine.board.legal_moves.count()
125125
init_board_pick_times.append(new_duration)
126126
revisit_pick_times.append(new_duration)

0 commit comments

Comments
 (0)