Running requests async in parallel
spidgorny opened this issue · 7 comments
In order to speed-up data retrieval it would be nice to be able to run several requests in parallel.
I know Guzzle supports it with their GuzzleHttp\Pool
class. I just need a way to create requests without sending them and then send them using the Pool
class.
Please let me know if there's an example or documentation for this.
I've started experimenting with it here: github-api-async
But I'm stuck and need help. Thanks.
Have a look at the Async feature that some HTTPlug clients have. http://docs.php-http.org/en/latest/components/promise.html#asynchronous-requests
If you are using, say Guzzle6, you may send request asynchronous. It may need some changes to the API client. Would you mind investigate in this?
Yes, that's what I need and attempted. I think I've managed to generate a new request with
$requestFactory = MessageFactoryDiscovery::find();
$request = $requestFactory->createRequest('get', $url, [], NULL);
Now I want to access the Guzzle client which is used inside Github\Client
. Maybe it's impossible (I don't have to) because I can inject Guzzle client into Github\Client
myself? Like here: customize.md.
Am I going in the right direction?
You can inject a client, yes. But we add lots of plugins at it. For authentication, caching etc.
Sorry for my ignorance about promises, but what is your plan? Sure we can send requests in parallel, but you are interested in the result, right? Maybe create a promise around the calls to the Github Client?
Maybe create a promise around the calls to the Github Client?
Hm, interesting idea. Not sure it will work. Calls like this
$client->api('repo')->show('user', 'repo');
are anyway synchronous. They will not run in parallel even if they are wrapped around in promises, IMHO.
You can inject a client, yes. But we add lots of plugins at it. For authentication, caching etc.
So, is there a way to get an instance of the HTTP client used by Github\Client
with all the plugins attached?
Yes, You may do
$github->getHttpClient();
This will always get you the latest client but it will be a HttpMethodsClient
, inside it there is a PluginClient
which supports Async..
Injecting my own Guzzle\Client
worked fine. An example can be checked at spidgorny/github-api-async.
$this->httpClient = new GuzzleClient([
'base_uri' => $this->base,
'proxy' => '',
]);
//$hc = $this->httpClient;
$hc = new AdapterClient($this->httpClient);
$this->client = GithubClient::createWithHttpClient(
new HttpMethodsClient($hc,
MessageFactoryDiscovery::find()
)
);
Running 30 requests with concurrency of 5 works just three times faster (not 5 times faster), somehow.
Thank you for the help.