Code Monkey home page Code Monkey logo

Comments (1)

maikelsalazar avatar maikelsalazar commented on June 15, 2024

This is not an issue, but here I give you an idea:

<?php

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;

class GeneratePool
{

    private ClientInterface $httpClient;
    private array $list;


    public function execute()
    {
        // populate $this->list ...
        $poolConfig = [
            'fulfilled' => function (ResponseInterface $response, $index): void {
                $this->fulfilled($response, $index);
            },
            'rejected' => function (Exception $reason, $index): void {
                $this->rejected($reason, $index);
            },
            'concurrency' => 50,
            'options' => [
                'connect_timeout' => 15,
                'exceptions' => true,
                'timeout' => 30,
            ],
        ];

        $pool = new Pool($this->httpClient, $this->generateRequests($this->list), $poolConfig);
        $promise = $pool->promise();
        $promise->wait();
    }

    private function generateRequests(): \Generator
    {
        foreach ($this->list as $index => $item) {
            // create the request and set a known index
            yield $index => new Request('GET', 'url', []);
        }
    }

    /**
     * @param ResponseInterface $response a successful response
     * @param $index the index set at the yield
     * @return void
     */
    private function fulfilled(ResponseInterface $response, $index): void
    {
        // this is a successful response from the Request yield a generateRequests
        // you have the response and do whatever you need to do with the item

        $item = $this->list[$index];
    }

    /**
     * @param Exception $reason this is an error response
     * @param $index the index set at the yield
     * @return void
     */
    private function rejected(\Exception $reason, $index): void
    {
        // $reason you might want to check reason->hasResponse() depends on the version of Guzzle you are working on
        // you have the response and do whatever you need to do with the item

        $item = $this->list[$index];
    }
}

from guzzle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.