Swoole Hook STDIO

Notice: the documentation is suitable for PHP extension swoole until v4.7.1, we only support PHP extension openswoole from v4.7.1

The SWOOLE_HOOK_STDIO flag will enable coroutine support for PHP standard IO. Support was added in v4.6.2.

More information on PHP STDIO can be found here.

Example

<?php

use Swoole\Process;

Co::set(['socket_read_timeout' => -1, 'hook_flags' => SWOOLE_HOOK_STDIO]);

$proc = new Process(function($p)
{
    Co\run(function() use($p)
    {
        $p->write('start'.PHP_EOL);

        go(function()
        {
            co::sleep(0.05);
            echo "sleep\n";
        });

        echo fread(STDIN, 1024);
    });

}, true, SOCK_STREAM);

$proc->start();

echo $proc->read();

usleep(100000);

$proc->write('hello world'.PHP_EOL);

echo $proc->read();
echo $proc->read();

Process::wait();