OpenSwoole Hook Proc

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

The OpenSwoole\Runtime::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' => OpenSwoole\Runtime::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