hyperf协程使用几种方式

swoole的协程是单进程单线程的,是不能利用多核的,想使用多核需要通过添加work数来实现。这里和go本质区别就是,worker内的全局函数是进程内共享的,全局共享需要通过共享内存等其他方式实现;

hyperf会为每个请求自动创建一个协程来处理,一个请求就是一个协程。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();
            },
        ]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值