Skip to content

Commit 0edd20a

Browse files
feat: use process pool instead
1 parent aeff38f commit 0edd20a

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

src/analyzer/modes.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from concurrent.futures import ThreadPoolExecutor
1+
import functools
2+
from concurrent.futures import ProcessPoolExecutor
23

34
from chess_utils import fen_to_tensor
45
from my_torch import Network
@@ -7,22 +8,23 @@
78
from .labels import vector_to_label
89

910

11+
def process_fen(network: Network, encoding: str, fen: str):
12+
try:
13+
output = network.predict(fen_to_tensor(fen, encoding=encoding))
14+
return vector_to_label(output)
15+
except Exception as e:
16+
return f"Error: {e}"
17+
18+
1019
def predict_mode(
1120
network: Network, chessfile: str, encoding: str = "simple"
1221
) -> None:
1322
fens = load_chessfile_predict(chessfile)
1423

15-
def process_fen(fen):
16-
try:
17-
x = fen_to_tensor(fen, encoding=encoding)
18-
output = network.predict(x)
19-
label = vector_to_label(output)
20-
return label
21-
except Exception as e:
22-
return f"Error: {e}"
23-
24-
with ThreadPoolExecutor(max_workers=4) as executor:
25-
results = executor.map(process_fen, fens)
24+
dispatch = functools.partial(process_fen, network, encoding)
25+
26+
with ProcessPoolExecutor(max_workers=4) as executor:
27+
results = executor.map(dispatch, fens)
2628

2729
for result in results:
2830
print(result)

0 commit comments

Comments
 (0)