已解决 GuzzleHttpPool 并发请求 没有生效,并发请求时间是 正常一次的叠加,很奇怪呀

tanhongbin

问题描述

按照文档中并发请求 使用的,结果确实时间叠加,并没有速度快
这里写问题描述

$client = new Client();

        $requests = function ($total) {
            $uri = 'http://www.baidu.com';
            for ($i = 0; $i < $total; $i++) {
                yield new GRequest('GET', $uri);
            }
        };

        $pool = new Pool($client, $requests(10), [
            'concurrency' => 10,
            'fulfilled' => function ($response, $index) {
                // this is delivered each successful response
            },
            'rejected' => function ($reason, $index) {
                // this is delivered each failed request
            },
        ]);

// Initiate the transfers and create a promise
        $promise = $pool->promise();

// Force the pool of requests to complete.
        $promise->wait();
742 2 1
2个回答

nitron

你那个GRequest里面是不是用requestAsync请求?

  • tanhongbin 2023-05-05

    这个文档中的源码 ,在webman中 有Request 冲突 所以用了 as 别名,就是文档中的源码,但是请求10次的时间 是 请求一次的时间 的10倍,发现并发没起作用

  • nitron 2023-05-05

    你那个GRequest里面是不是用requestAsync请求?

    
    $requests = function () use ($client, $total) {
        $uri = 'http://www.baidu.com';
            for ($i = 0; $i < $total; $i++) {
                 yield function() use ($client, $uri) {
                return $client->requestAsync('GET', $uri);
                 };
            }
    };
    
  • tanhongbin 2023-05-05

    不是呀,这个是文档中的 yield new Request('GET', $uri); Request 不是我封装的,这个和webman 的support\Request有名字冲突,就起了个别名

  • nitron 2023-05-05

    用你代码试了,没这个叠加的问题
    本机测试,total = 1 , 耗时70ms, total 1-10 耗时 350ms . total 10以上 约等于 total / 10 * 350

  • tanhongbin 2023-05-05

    这不就是叠加嘛,并发请求应该是 total=1 和total = 10 时间差不多的

  • nitron 2023-05-05

    我concurrency用的5

  • nitron 2023-05-05

    total10的时候,concurrency用的1-10都差不多时间,你调整total和concurrency,你会发现到达一个阈值的时候,就是叠加,猜测是本身并concurrency有上限

  • tanhongbin 2023-05-05

    按照道理来说,请求一次和请求五次 应该是时间一致的,不会相差很大

  • tanhongbin 2023-05-05

    你可以测试一下请求一次和请求五次,concurrency 设置成5试试,如果时间基本一致,那就是对了,如果还是时间叠加,那就不对

  • lobtao 2023-07-27

    用swoole或者workerman 原始的方式试试

小W

这就是官方的示例,最后怎么解决的

🔝