0

I had success running concurrent HTTP requests with this code:

function do_requests(\GuzzleHttp\Client $client, int $total): \Generator
{
    for ($i = 0; $i < $total; $i++) {
        $uri = "https://httpbin.org/anything?q=$i";
        $promise = $client->getAsync($uri);
        yield ['uri' => $uri, 'response' => $promise->wait()->getBody()->getContents()];
    }
}

$client = new \GuzzleHttp\Client();

foreach (do_requests($client, 100) as $do_request) {
    var_dump($do_request);
}

However, I must ensure a maximum of 25 concurrent requests, so I tried this code:

function prepare_requests(int $total): \Generator
{
    for ($i = 0; $i < $total; $i++) {
        $uri = "https://httpbin.org/anything?q=$i";
        yield new \GuzzleHttp\Psr7\Request('GET', $uri);
    }
}

function do_pool_requests(\GuzzleHttp\Client $client, int $total): \Generator
{
    $pool = new \GuzzleHttp\Pool($client, prepare_requests($total), ['concurrency' => 25]);

    /** @var \GuzzleHttp\Psr7\Response $response */
    foreach ($pool->promise()->wait() as $response) {
        yield ['headers' => $response->getHeaders(), 'response' => $response->getBody()->getContents()];
    }
}

$client = new \GuzzleHttp\Client();

foreach (do_pool_requests($client, 100) as $do_request) {
    var_dump($do_request);
}

This second example fails, though. It raises the warning PHP Warning: foreach() argument must be of type array|object, null given and points to this line: foreach ($pool->promise()->wait() as $response) {.

What am I doing wrong?

VBobCat
  • 2,527
  • 4
  • 29
  • 56
  • I guess you have understood pool requests wrong, I suggest to read docs again, if you want help follow [this answer](https://stackoverflow.com/a/65747999/9471283), If you like the don't forget to upvote – bhucho Oct 26 '21 at 19:01
  • Thank you, I'll try. What I really need is the abilty to iterate through the responses as soon as they get available. – VBobCat Oct 26 '21 at 19:11
  • @bhucho, I went back to the docs, where `Pool` is mentioned only once (and IMHO very lousily) [here](https://docs.guzzlephp.org/en/stable/quickstart.html#concurrent-requests). I need to implement in PHP a concurrency-limited generator [I already have a Python prototype](https://stackoverflow.com/a/69691510/3718031) for, as a POC. I thought `Pool` would be the way to go, but I don't know how to extract completed responses from it. – VBobCat Oct 26 '21 at 19:35
  • `but I don't know how to extract completed responses from it.` define an external variable or array and store the data in it. see [this example answer](https://stackoverflow.com/a/64924881/9471283) – bhucho Oct 27 '21 at 08:39
  • If it does not work then I will try to write an answer to help out – bhucho Oct 27 '21 at 08:41

0 Answers0