由于在协程空间内 fork 进程会带着其他协程上下文,因此底层禁止了在 Coroutine 中使用 Process 模块。可以使用
System::exec() 或 Runtime Hook+shell_exec 实现外面程序运行
Runtime Hook+proc_open 实现父子进程交互通信
use Swoole\Runtime;
use function Swoole\Coroutine\run;
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
run(function () {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/tmp/error-output.txt", "a")
);
$process = proc_open('php ' . __DIR__ . '/read_stdin.php', $descriptorspec, $pipes);
$n = 10;
while ($n--) {
fwrite($pipes[0], "hello #$n \n");
echo fread($pipes[1], 8192);
}
fclose($pipes[0]);
proc_close($process);
});while(true) {
$line = fgets(STDIN);
if ($line) {
echo $line;
} else {
break;
}
}