Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
The SWOOLE_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.
<?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();