swoole的协程是单进程单线程的,是不能利用多核的,想使用多核需要通过添加work数来实现。这里和go本质区别就是,worker内的全局函数是进程内共享的,全局共享需要通过共享内存等其他方式实现;
hyperf封装的协程基本有四种方式,第一种就是go或co关键字,通过管道channel通讯来并行处理;第二种是通过waitgroup;前两种发现和go几乎一样,第三种通过Parallel;第四种使用Parallel的全局函数,其实都是对前面两种的封装
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\Parallel;
use Hyperf\Utils\WaitGroup;
use Swoole\Coroutine\Channel;
/**
* @AutoController
*/
class IndexController extends AbstractController
{
/**
* @Inject
* @var \Hyperf\Guzzle\ClientFactory
*/
private $clientFactory;
public function index()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
return [
'method' => $method,
'message' => "Hello {$user}.",
];
}
public function sleep(RequestInterface $request)
{
$seconds = $request->input('seconds', 1);
sleep((int) $seconds);
return $seconds;
}
public function test()
{
// $channel = new Channel();
// co(function () use ($channel) {
// $client = $this->clientFactory->create();
// $res = $client->get('127.0.0.1:9501/index/sleep?seconds=3');
// $channel->push(123);
// });
// co(function () use ($channel) {
// $client = $this->clientFactory->create();
// $res = $client->get('127.0.0.1:9501/index/sleep?seconds=2');
// $rs = $res->getBody()->getContents();
// $channel->push($rs);
// });
// $result = [];
// $result[] = $channel->pop();
// $result[] = $channel->pop();
//第二种方式
// $wg = new WaitGroup();
// $result = [];
// $wg->add(2);
// $client = $this->clientFactory->create();
// co(function () use ($wg, &$result, $client) {
// $res = $client->get('127.0.0.1:9501/index/sleep?seconds=3');
// $rs = $res->getBody()->getContents();
// $result[] = $rs;
// $wg->done();
// });
// co(function () use ($wg, &$result, $client) {
// $res = $client->get('127.0.0.1:9501/index/sleep?seconds=2');
// $rs = $res->getBody()->getContents();
// $result[] = $rs;
// $wg->done();
// });
// $wg->wait();
//第三种方式
// $parall = new Parallel();
// $parall->add(function () {
// $client = $this->clientFactory->create();
// $res = $client->get('127.0.0.1:9501/index/sleep?seconds=3');
// return $res->getBody()->getContents();
// });
// $parall->add(function () {
// $client = $this->clientFactory->create();
// $res = $client->get('127.0.0.1:9501/index/sleep?seconds=2');
// return $res->getBody()->getContents();
// });
// return $parall->wait();
//第四种
return parallel([
'a' => function () {
$client = $this->clientFactory->create();
$res = $client->get('127.0.0.1:9501/index/sleep?seconds=3');
return $res->getBody()->getContents();
},
'b' => function () {
$client = $this->clientFactory->create();
$res = $client->get('127.0.0.1:9501/index/sleep?seconds=2');
return $res->getBody()->getContents();
},
]);
}
}