Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

并发怎么获取返回值 #6

Open
yuanzhihai opened this issue Aug 17, 2021 · 0 comments
Open

并发怎么获取返回值 #6

yuanzhihai opened this issue Aug 17, 2021 · 0 comments

Comments

@yuanzhihai
Copy link

yuanzhihai commented Aug 17, 2021

 $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);

@GeGeLv GeGeLv mentioned this issue Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant