Spaces:
Sleeping
Sleeping
Update main.py
Browse files126 unmodified lines127128129130131132110 unmodified lines24324424524624724824925025125241 unmodified lines294295296297298299300301302303183 unmodified lines48748848949049149249341 unmodified lines535536537538539540541126 unmodified lines# Global engine instances to save memory and improve performance_GLOBAL_DEEPCASTLE_ENGINE = None_ENGINE_LOCK = asyncio.Lock()def _engine_hash_mb() -> int:110 unmodified lineslimit = chess.engine.Limit(time=request.time, depth=request.depth)# One search: stats must come from this run (not a separate short analyse).result = await engine.play(board, limit, info=chess.engine.INFO_ALL)info = dict(result.info)if not info:info = await engine.analyse(board, limit, info=chess.engine.INFO_ALL)# From White's perspective in CP -> converted to Pawns for UIscore_cp, mate_in = get_normalized_score(info)41 unmodified linesboard = chess.Board(request.fen)limit = chess.engine.Limit(time=request.time, depth=request.depth)result = await engine.play(board, limit, info=chess.engine.INFO_ALL)info = dict(result.info)if not info:info = await engine.analyse(board, limit, info=chess.engine.INFO_ALL)score_cp, mate_in = get_normalized_score(info)183 unmodified linesanalysis_results = []infos_before = await engine.analyse(board, limit, multipv=2)infos_before = infos_before if isinstance(infos_before, list) else [infos_before]counts = {41 unmodified linesmove_history.append(move)fen_history.append(board.fen())infos_after_raw = await engine.analyse(board, limit, multipv=2)infos_after: List[dict] = infos_after_raw if isinstance(infos_after_raw, list) else [infos_after_raw]info_after_dict: dict = infos_after[0]126 unmodified lines127128129130131132133134135110 unmodified lines24624724824925025125225325425525641 unmodified lines298299300301302303304305306307308183 unmodified lines49249349449549649749849941 unmodified lines541542543544545546547548126 unmodified lines# Global engine instances to save memory and improve performance_GLOBAL_DEEPCASTLE_ENGINE = None_ENGINE_LOCK = asyncio.Lock()# UCI engines handle one search at a time; concurrent play/analyse on the same process# corrupts the protocol and can crash the binary — serialize all I/O._ENGINE_IO_LOCK = asyncio.Lock()def _engine_hash_mb() -> int:110 unmodified lineslimit = chess.engine.Limit(time=request.time, depth=request.depth)# One search: stats must come from this run (not a separate short analyse).async with _ENGINE_IO_LOCK:result = await engine.play(board, limit, info=chess.engine.INFO_ALL)info = dict(result.info)if not info:info = await engine.analyse(board, limit, info=chess.engine.INFO_ALL)# From White's perspective in CP -> converted to Pawns for UIscore_cp, mate_in = get_normalized_score(info)41 unmodified linesboard = chess.Board(request.fen)limit = chess.engine.Limit(time=request.time, depth=request.depth)async with _ENGINE_IO_LOCK:result = await engine.play(board, limit, info=chess.engine.INFO_ALL)info = dict(result.info)if not info:info = await engine.analyse(board, limit, info=chess.engine.INFO_ALL)score_cp, mate_in = get_normalized_score(info)183 unmodified linesanalysis_results = []async with _ENGINE_IO_LOCK:infos_before = await engine.analyse(board, limit, multipv=2)infos_before = infos_before if isinstance(infos_before, list) else [infos_before]counts = {41 unmodified linesmove_history.append(move)fen_history.append(board.fen())async with _ENGINE_IO_LOCK:infos_after_raw = await engine.analyse(board, limit, multipv=2)infos_after: List[dict] = infos_after_raw if isinstance(infos_after_raw, list) else [infos_after_raw]info_after_dict: dict = infos_after[0]
main.py+17-10