PHP Conference Ehime 2026

Yar_Concurrent_Client::loop

(PECL yar >= 1.0.0)

Yar_Concurrent_Client::loopSend and wait for all registered calls

Descrizione

public static function Yar_Concurrent_Client::loop(?callable $callback = null, ?callable $error_callback = null, ?array $options = null): ?bool

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().

Elenco dei parametri

callback

A callable used 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_callback

A callable used to handle errors for calls that were registered without their own error callback. It receives three arguments: the error type (one of the YAR_ERR_* codes), the error message, and the callinfo array.

If this parameter is omitted, a failed call raises a PHP warning instead; unlike the synchronous Yar_Client, no exception is thrown.

options

An array of client options, keyed by the YAR_OPT_* constants, applied to every registered call that does not override them.

Valori restituiti

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.

Esempi

Example #1 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");
?>

Il precedente esempio visualizzerà qualcosa simile a:

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"

Vedere anche:

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top