swoole一键协程化(proc函数)(shell命令、bat命令执行)

2021-10-31

一键协程化,包括了:proc_open、proc_close、proc_get_status、proc_terminate

use Swoole\Coroutine;
use function Swoole\Coroutine\run;

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

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 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;
    }
});

 

{/if}