OpenSwoole Hook STDIO

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

The OpenSwoole\Runtime::HOOK_STDIO flag will enable coroutine support for PHP standard IO. Support was added in OpenSwoole 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' => OpenSwoole\Runtime::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();
Last updated on August 31, 2022