普通方式请求两次`sleep`方法需要`4`秒左右,而创建协程则只需要`2`秒左右 ####使用`co`方法创建 ``` <?php declare(strict_types=1); namespace App\Controller; use Hyperf\Di\Annotation\Inject; use Hyperf\Guzzle\ClientFactory; use Hyperf\HttpServer\Annotation\AutoController; use Hyperf\HttpServer\Contract\RequestInterface; use Swoole\Coroutine\Channel; /** * @AutoController() */ class CoController{ /** * @Inject() * @var ClientFactory */ private $clientFactory; public function sleep(RequestInterface $request){ $time = $request->input('time',2); $time = (int)$time; sleep($time); return $time; } public function test(){ $channel = new Channel(); co(function () use ($channel) { $client = $this->clientFactory->create(); $client->get('http://hyperf.qinxuihui.cn:9501/co/sleep?time=2'); $channel->push(123); }); co(function () use ($channel) { $client = $this->clientFactory->create(); $client->get('http://hyperf.qinxuihui.cn:9501/co/sleep?time=2'); $channel->push(321); }); $result[] = $channel->pop(); $result[] = $channel->pop(); return $result; } } ``` ####使用`WaitGroup`创建 ``` <?php declare(strict_types=1); namespace App\Controller; use Hyperf\Di\Annotation\Inject; use Hyperf\Guzzle\ClientFactory; use Hyperf\HttpServer\Annotation\AutoController; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\Utils\WaitGroup; /** * @AutoController() */ class CoController{ /** * @Inject() * @var ClientFactory */ private $clientFactory; public function sleep(RequestInterface $request){ $time = $request->input('time',2); $time = (int)$time; sleep($time); return $time; } public function group(){ $wg = new WaitGroup(); $result = []; $wg->add(2); //协程的数量 co(function () use ($wg,&$result) { $client = $this->clientFactory->create(); $client->get('http://hyperf.qinxuihui.cn:9501/co/sleep?time=2'); $result[] = 123; $wg->done(); }); co(function () use ($wg,&$result) { $client = $this->clientFactory->create(); $client->get('http://hyperf.qinxuihui.cn:9501/co/sleep?time=2'); $result[] = 321; $wg->done(); //此方法代表该协程已经执行完毕了 }); $wg->wait(); //此方法代表等待上方协程执行完毕 return $result; } } ``` 使用`Parallel`创建 ``` <?php declare(strict_types=1); namespace App\Controller; use Hyperf\Di\Annotation\Inject; use Hyperf\Guzzle\ClientFactory; use Hyperf\HttpServer\Annotation\AutoController; use Hyperf\HttpServer\Contract\RequestInterface; /** * @AutoController() */ class CoController{ /** * @Inject() * @var ClientFactory */ private $clientFactory; public function sleep(RequestInterface $request){ $time = $request->input('time',2); $time = (int)$time; sleep($time); return $time; } public function parallel(){ /** * key1可写可不写,如果写了那么则返回值的键也为key1 */ $result = parallel([ 'key1' => function (){ $client = $this->clientFactory->create(); $client->get('http://hyperf.qinxuihui.cn:9501/co/sleep?time=2'); return 123; }, 'key2'=> function (){ $client = $this->clientFactory->create(); $client->get('http://hyperf.qinxuihui.cn:9501/co/sleep?time=2'); return 321; } ]); return $result; } } $result = [ 'key1' => 123, 'key2 => 321 ] ``` 非特殊说明,本文版权归秦官人所有,转载请注明出处. 0 本文标题: hyperf 创建协程的几种方式 延伸阅读 上一篇: hyperf 协程上下文 下一篇: windows10 安装过程出现计算机意外的重新启动或遇到错误 发表评论 说点什么吧 | 0评论 最新 最早 还没有评论,快来抢沙发吧!