【发布时间】:2015-10-27 07:44:12
【问题描述】:
我正在考虑制作一个 php 脚本,它可以打开 stockfish 国际象棋引擎 CLI,发送一些命令并取回输出。
我想我可以通过使用带有管道数组的proc_open 来实现这一点,但我不知道如何等待整个输出......如果有另一个更好的解决方案,不胜感激!
这是我的代码:
// ok, define the pipes
$descr = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$pipes = array();
// open the process with those pipes
$process = proc_open("./stockfish", $descr, $pipes);
// check if it's running
if (is_resource($process)) {
// send first universal chess interface command
fwrite($pipes[0], "uci");
// send analysis (5 seconds) command
fwrite($pipes[0], "go movetime 5000");
// close read pipe or STDOUTPUT can't be read
fclose($pipes[0]);
// read and print all output comes from the pipe
while (!feof($pipes[1])) {
echo fgets($pipes[1]);
}
// close the last opened pipe
fclose($pipes[1]);
// at the end, close the process
proc_close($process);
}
进程似乎开始了,但是我发送给进程的第二个 STDINPUT 不能等到它完成,因为第二个命令会产生大约 5 秒的分析行,并且它立即打印出结果。 怎么办?
CLI link, CLI documentation link
如果您需要,请向我询问有关此引擎的更多信息。
谢谢!
【问题讨论】: