Search code examples
pythonchessuci

Running stockfish in infinite analysis in the background in python-chess


I wrote a chess program with very nice GUI (PyQt5). When I enter a move it analyzes it and updates the SVG representation of the board - thanks to wonderful python-chess module. Everything works fine now. But, what I want to do is to let the engine work in background and infinitely analyze the board allowing me to enter new moves. Here is a simple code example:

import asyncio
import chess
import chess.engine
board = chess.Board()

async def analyse():
    transport, engine = await chess.engine.popen_uci("./stockfish-10-64")
    board = chess.Board()
    info = await engine.analyse(board, chess.engine.Limit(time=2))
    print(info["score"])
    await engine.quit()
    return(info)

async def get_input():
    a = input("enter move in SAN format")
    board.push_san(a)
    print(board)
    xx = await analyse()
    print(xx)

while(True):
    asyncio.run(get_input())

In this example, I cannot enter a new move before the analysis has been done. (Note: In the original design moves are entered in PyQt5 "lineedit" widget, do not worry about the difficulties in Asynchronous terminal input)

Thanks,


Solution

  • You could use 'ponder' property of engine.play, which allows for analysis in the background. Now you can break your 2 second search into chunks and be able to input move in-between getting similar quality of search.