How to Communicate with a Chess engine in Python?

Nils Lindemann picture Nils Lindemann · Sep 9, 2012 · Viewed 9.2k times · Source

on win 7 i can communicate with a chess engine via commandline. Small Example Session with Stockfish on Win 7:

C:\run\Stockfish>stockfish-x64.exe
Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski
quit

C:\run\Stockfish>

The first line was output by the engine and the 'quit' was what i typed to quit the engine (There are other things i can do, but thats clear to me).

Now i want to communicate with that engine from python:

import subprocess
engine = subprocess.Popen(
    'stockfish-x64.exe',
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
)
for line in engine.stdout:
    print(line.strip())
engine.stdin.write('quit\n')

and i get

C:\run\Stockfish>communicate.py
b'Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski'

but it doesnt quit the engine (no C:\run\Stockfish> prompt), it keeps waiting for input. I have to close the window by hand. It seems not to get my quit message (last line of the python script) written to stdin.

In other words i can read from stdout but when i write to stdin nothing happens.

What am i doing wrong and how to do it right?


Edit: ok, thanks to larsmans´ help i solved it:

Example Python script:

import subprocess, time

engine = subprocess.Popen(
    'stockfish-x64.exe',
    universal_newlines=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    bufsize=1,
)

def put(command):
    print('\nyou:\n\t'+command)
    engine.stdin.write(command+'\n')
    
def get():
    # using the 'isready' command (engine has to answer 'readyok')
    # to indicate current last line of stdout
    engine.stdin.write('isready\n')
    print('\nengine:')
    while True:
        text = engine.stdout.readline().strip()
        if text == 'readyok':
            break
        if text !='':
            print('\t'+text)

get()
put('uci')
get()
put('setoption name Hash value 128')
get()
put('ucinewgame')
get()
put('position startpos moves e2e4 e7e5 f2f4')
get()
put('go infinite')
time.sleep(3)
get()
put('stop')
get()
put('quit')

Output:

C:\run\Stockfish>1-communicate.py

engine:
        Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski

you:
        uci

engine:
        id name Stockfish 2.2.2 JA SSE42
        id author Tord Romstad, Marco Costalba and Joona Kiiski
        option name Use Search Log type check default false
        option name Search Log Filename type string default SearchLog.txt
        ... etc ...
        uciok

you:
        setoption name Hash value 128

engine:

you:
        ucinewgame

engine:

you:
        position startpos moves e2e4 e7e5 f2f4

engine:

you:
        go infinite

engine:
        info depth 1 seldepth 1 score cp 56 nodes 62 nps 1675 time 37 multipv 1 pv e5f4
        info depth 2 seldepth 2 score cp 48 nodes 804 nps 21157 time 38 multipv 1 pv b8c6 g1h3
        info depth 3 seldepth 3 score cp 64 nodes 1409 nps 37078 time 38 multipv 1 pv b8c6 b1c3 e5f4
        ... etc ...

you:
        stop

engine:
        bestmove e5f4 ponder g1f3

you:
        quit

C:\run\Stockfish>

Answer

Fred Foo picture Fred Foo · Sep 9, 2012

You've got a deadlock: the subprocess is waiting for input, while your program is waiting for it to output more lines in

for line in engine.stdout:
    print(line.strip())

This loop only stops when the subprocess closes its stdout.