class Parallel { private array $threads = []; private array $process = []; public function add(PromiseInterface $promise): self { $this->threads[] = $promise; return $this; } public function run(): void { foreach ($this->threads as $thread) { $descriptors = [ 0 => ['pipe', 'r'], // stdin 1 => ['pipe', 'w'], // stdout 2 => ['pipe', 'w'], // stderr ]; $process = proc_open('php', $descriptors, $pipes); if (is_resource($process)) { fwrite($pipes[0], $this->generatePhpCode($thread)); fclose($pipes[0]); fclose($pipes[2]); $this->process [] = [ 'pipe' => $pipes[1], 'process' => $process, 'thread' => $thread ]; } } } /*** * @return void */ public function wait(): void { while (!empty($this->process)) { foreach ($this->process as $key => $value) { [ 'pipe' => $pipe, 'process' => $process, 'thread' => $thread ] = $value; if (is_resource($process)) { $status = proc_get_status($process); if ($status['running']) { continue; } /** * @var PromiseInterface $thread */ $thread->then(stream_get_contents($pipe)); echo PHP_EOL; proc_close($process); unset($this->process[$key]); } } } } /*** * @param PromiseInterface $promise * @return string */ private function generatePhpCode(PromiseInterface $promise): string { $serialize=serialize($promise); return ""; } }