I'm using Symfony 3 and ReactPHP library for control all my functionality and I need to execute multiple calls to same function (subFunction()
in code) asynchronously.
I have 2 projects (project1 and project2):
Project 1 code:
/**
* Loop an array of urls and call sub function.
**/
public function startFunction() {
$finalResponse = [];
$urls = ['www.google.es', 'www.github.com', 'www.bitbucket.org'];
foreach ($urls as $url) {
$res = $this->subFunction($url); // subfunction call ( **IT MAY TAKE A LONG TIME !!** )
$finalResponse[] = $res;
}
return $finalResponse;
}
/**
* Uses Factory loop to get the Promise returned by finalRequest function.
**/
private function subFunction($url) {
$loop = \React\EventLoop\Factory::create();
$classA = new Project2\ClassA();
$finalResponse = null;
// project 2 function call
$classA->finalRequest($url)->then(function($response) use(
&$finalResponse
) {
$finalResponse = $response;
})
return $finalResponse;
}
Project 2 code:
classA {
/**
* Makes an React\HttpClient request (GET) to sent url and return his value inside a Promise.
**/
public function finalRequest($url) {
$generalDeferred = new Deferred();
$generalPromise = $generalDeferred->promise();
// make React\HttpClient request
$request = $client->request('GET', $url);
$request->on('response', function ($response) use($generalDeferred) {
$response->on('data', function ($response) {
$generalDeferred->resolve($response);
});
});
$request->end();
return $generalPromise;
}
}
Problem is that on every subFunction($url)
call, the program stops until the sub Function gets the response, but I need to do this asynchronously because this subFunction could take many seconds.
So I would like to launch all subFunction($url)
calls at the same time, and get all responses asynchronously.
It's possible solve this problem? Thanks.