Instead of calling services one after another,
Yar_Concurrent_Client registers several calls
first and then dispatches them all at once with
Yar_Concurrent_Client::loop(). The responses
are passed to the callback in the order they arrive, not in the
order the calls were registered.
Right after all requests have been sent, the callback is invoked
once with null arguments so that the caller knows no further
request is pending; the example below checks for this notification.
<?php
function callback($ret, $callinfo) {
if ($callinfo == NULL) {
/* all requests are sent, waiting for the responses */
return;
}
echo $callinfo['method'], " result: ", $ret, "\n";
}
function error_callback($type, $error, $callinfo) {
error_log("[$type] $error");
}
/* register async calls to remote services */
Yar_Concurrent_Client::call("http://api.example.com/operator.php", "add", array(1, 2), "callback");
Yar_Concurrent_Client::call("http://api.example.com/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::call("http://api.example.com/operator.php", "mul", array(2, 2), "callback");
/* send all requests and wait for the responses */
Yar_Concurrent_Client::loop("callback", "error_callback");
?>
The above example will output
something similar to:
mul result: 4
sub result: 1
add result: 3