Coroutine
版本更新
在4.4.4
版本中系统操作相关的协程API
从Coroutine
类中,迁移到了Coroutine\System
类中。独立为一个新模块。为了向下兼容,底层依然保留了在Coroutine
类之上的别名方法。
Coroutine::sleep
对应Coroutine\System::sleep
Coroutine::fgets
对应Coroutine\System::fgets
短名称
在2.0.13
与2.1.0
或更高版本中,增加了协程短名特性,简化了协程相关API
的名称书写。可修改php.ini
设置swoole.use_shortname
来关闭/开启短名,默认为开启。
创建协程
go(function () {
co::sleep(0.5);
echo "hello";
});
go("test");
go([$object, "method"]);
通道操作
$c = new chan(1);
$c->push($data);
$c->pop();
协程客户端
$redis = new Co\Redis;
$mysql = new Co\MySQL;
$http = new Co\Http\Client;
$tcp = new Co\Client;
$http2 = new Co\Http2\Client;
系统操作
System::sleep(100);
System::fread($fp);
System::gethostbyname('www.baidu.com');
延迟执行
defer(function () use ($db) {
$db->close();
});