easyhttp icon indicating copy to clipboard operation
easyhttp copied to clipboard

并发怎么获取返回值

Open yuanzhihai opened this issue 4 years ago • 0 comments

 $data  = [];
        $promises=[];
        Http::concurrency(10)->multiAsync($promises, function (Response $response, $index) use(&$data) {
            $data[]= $response->body();
        }, function (RequestException $e, $index) {
            echo "发起第 $index 个请求失败,失败原因:" . $e->getMessage() . PHP_EOL;
        });
        print_r($data);

返回空

GuzzleHttp 是没问题的

$requests = function () use ($array) {
            $uri = '';
            foreach ($array as $val) {
                yield new Request('GET', $uri );
            }
        };
 $pool     = new Pool(
            $client, $requests(), [
                       'concurrency' => 10, //同时执行
                       'fulfilled'   => function ($response, $index) use (&$data, &$error) {
                           $res          = json_decode($response->getBody()->getContents(),1);
                           $data+=$res['data'];
                       },
                       'rejected'    => function ($reason, $index) {
                           // 失败的响应
                           $error = $reason;
                       },
                   ]
        );
        $promise  = $pool->promise();
        $promise->wait();
print_r($data);

yuanzhihai avatar Aug 17 '21 09:08 yuanzhihai