Swoole Hook Proc

The SWOOLE_HOOK_PROC flag will enable coroutine support for proc_* PHP functions. Support was added in v4.4.0.

This includes support for:

  • proc_open
  • proc_close
  • proc_get_status
  • proc_terminate

More info here.

Example

<?php

Co::set(['hook_flags' => SWOOLE_HOOK_PROC]);

Co\run(function () {

    $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin, child process read from it
        1 => array("pipe", "w"),  // stdout, child process write to it
    );

    $process = proc_open('php', $descriptorspec, $pipes);

    if (is_resource($process))
    {

        fwrite($pipes[0], '<?php echo "I am a process\n" ?>');
        fclose($pipes[0]);

        while(true)
        {
            echo fread($pipes[1], 1024);
        }

        fclose($pipes[1]);
        $return_value = proc_close($process);

        echo "command returned $return_value" . PHP_EOL;
    }
});