OpenSwoole Hook Proc

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

The SWOOLE_HOOK_PROC flag will enable coroutine support for proc_* PHP functions. Support was added in OpenSwoole 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;
    }
});
Last updated on August 31, 2022