Coroutine

协程设置

可使用Coroutine::set设置协程相关选项。

Swoole\Coroutine::set(array(
	'max_coroutine' => 4096,
));

max_coroutine

设置最大协程数,超过限制后底层将无法创建新的协程。

stack_size

设置单个协程初始栈的内存尺寸,默认为2M

短名称

2.0.132.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;

其他 API

co::sleep(100);
co::fread($fp);
co::gethostbyname('www.baidu.com');