D
D
Dark_Dante2017-11-22 17:30:41
CodeIgniter
Dark_Dante, 2017-11-22 17:30:41

How to catch asynchronous php?

Hello.
In general, I play around with the asynchronous execution of php code. The bottom line is ... There is a script, it refers to third-party web services. There are many services, their response time is different. If you synchronously execute each request to the web service, then it takes a lot of time ...
There is a library https://github.com/bucefal91/php-async from the respected https://habrahabr.ru/users/bucefal91/. Using this library, I make an asynchronous call to web services, something like this

$webservices=['service1', 'service2', 'service3'];  //веб-сервисов может быть произвольное количество
$service=[];
foreach($webservices as $k=>$v){
      $service[$v]=new ToolsAsyncResult('php index.php ' . $v); //делаем запрос к каждому сервису
}

An instance of the ToolsAsyncResult class has an isRunning() method - which returns true during script execution and false if a result is received.
For one asynchronous call, catching the result is not a problem - the author did it like this
//тут мы вызваем ассинхронный запрос
$foo=new ToolsAsyncResult('php index.php servicename' ); 
while($foo->isRunning()){
//крутим цикл пока не придет ответ от сервиса
}
$result=$foo->result(); //получаем результат и как то его обрабатываем

How to catch the moment when all instances of the ToolsAsyncResult class return data?
do this way
while($service[0]->isRunning() AND $service[1]->isRunning() AND $service[3]->isRunning()){
//крутим цикл пока не придет ответ от сервиса
}

I can't, because the number of running calls can be arbitrary.
Please help my grief

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question