Not able to communicate with Katago in stdout
Hi all,
I'm trying to write a python script to make it easier for blind users to play with katago in command lines,so that the blind user can simply type , e.g, C15 in python command line instead of play w C15 to play .
here is the script:
import subprocess
location = 'C:\\Users\\視障教育輔具中心\\Desktop\\katago-v1.10.0-opencl-windows-x64\\katago.exe gtp -model C:\\Users\\視障教育輔具中心\\Desktop\\katago-v1.10.0-opencl-windows-x64\\kata1-b40c256-s10568986624-d2575992043.bin.gz -config C:\\Users\\視障教育輔具中心\\Desktop\\katago-v1.10.0-opencl-windows-x64\gtp_custom.cfg'
class Engine():
def __init__(self):
self.engine = subprocess.Popen(
location,
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
bufsize=1
)
while True:
text = self.engine.stdout.readline()
if 'main' in text:
break
self.put('time_settings 10 5 1')
def put(self, command):
print(command)
self.engine.stdin.write(command + "\n")
engine = Engine()
inp = input('黑方或白方? b or w ?')
side = 'black' if inp == 'b' else 'white'
print('you play'+side+',please enter your move')
if side == 'black':
while True:
engine.put('play b'+input())
engine.put('genmove w')
print(engine.engine.stdout.readline())
else:
while True:
engine.put('genmove b')
engine.put('play w'+input())
print(engine.engine.stdout.readline())
but the whole script stop at However, the whole script stuck at
self.engine = subprocess.Popen(
location,
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
bufsize=1
)
because katago will go into it's main loop could anyone help me with this ?
Katago logs most information to stderr. You probably want to output that as well. I'm also using bufsize=0 with my script which works.
As you use Python, you can look the code of KaTrain:
https://github.com/sanderland/katrain/blob/070420941c117e2ca2fdff8e0c7521566149e760/katrain/core/engine.py#L149-L156
And KataGo's stdout is enough, one can code a whole KataGo GUI without stderr as far as I know, but you need to close the startup stderr info:
https://github.com/lightvector/KataGo/blob/master/cpp/configs/gtp_example.cfg#L57-L59