On Windows 7 I can communicate with a chess engine via command line. 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 that's 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 doesn't 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?