Yar_Concurrent_Client::loop
(PECL yar >= 1.0.0)
Send and wait for all registered calls
Description
Sends all calls registered with Yar_Concurrent_Client::call in parallel and blocks until every response has arrived and been handled. The call list is emptied when this method returns.
Because the calls run simultaneously, the loop takes only as long as the slowest single call rather than the sum of all calls. The callbacks, however, are not invoked in the order the calls were registered: a callback fires as soon as its response arrives. To find out which call a response belongs to, check the sequence entry of the callinfo argument against the sequence number returned by Yar_Concurrent_Client::call. The callinfo Array holds the sequence, uri and method of the call; see Yar_Concurrent_Client::call.
Parameters
callbackA
callableused to handle responses for calls that were registered without their own callback. Right after all requests have been sent, it is additionally called once with two null arguments, before any response arrives.If this parameter is omitted, the return value of a successful call is simply printed when its response arrives.
error_callbackA
callableused to handle errors for calls that were registered without their own error callback. It receives three arguments: the error type (one of theYAR_ERR_*codes), the error message, and thecallinfoArray.If this parameter is omitted, a failed call raises a PHP warning instead; unlike the synchronous
Yar_Client, no exception is thrown.optionsAn Array of client options, keyed by the
YAR_OPT_*constants, applied to every registered call that does not override them.
Return Values
Returns true when every registered call has been handled, or when no calls have been registered at all. Returns false if the concurrent client is already running inside another loop; in that case a warning is raised.
Examples
Yar_Concurrent_Client::loop example
<?php
function callback($retval, $callinfo) {
if ($callinfo == NULL) {
echo "Now, all requests are sent, and no response available\n";
} else {
echo "This is a remote call response, the method name is ", $callinfo["method"],
". calling sequence is ", $callinfo["sequence"], "\n";
var_dump($retval);
}
}
function error_callback($type, $error, $callinfo) {
error_log($error);
}
Yar_Concurrent_Client::call("http://api.example.com/operator.php", "some_method", array("parameters"), "callback");
/* if the callback is not specified, the callback of loop() will be used */
Yar_Concurrent_Client::call("http://api.example.com/operator.php", "some_method", array("parameters"));
Yar_Concurrent_Client::loop("callback", "error_callback");
?>The above example will output something similar to:
Now, all requests are sent, and no response available
This is a remote call response, the method name is some_method. calling sequence is 2
string(11) "some_method"
This is a remote call response, the method name is some_method. calling sequence is 1
string(11) "some_method"See Also
Yar_Concurrent_Client::callYar_Concurrent_Client::reset