PHP 8.1.28 Released!

Yar_Concurrent_Client::loop

(PECL yar >= 1.0.0)

Yar_Concurrent_Client::loop发送所有注册的并行调用

说明

public static Yar_Concurrent_Client::loop(callable $callback = ?, callable $error_callback = ?): bool

发送所有的已经通过 Yar_Concurrent_Client::call() 注册的并行调用, 并且等待返回.

参数

callback

如果这个回掉函数被设置, 那么 Yar 在发送出所有的请求之后立即调用一次这个回掉函数(此时还没有任何请求返回), 调用的时候 $callinfo 参数是 NULL。

如果在注册并行调用的时候制定了 callback, 那么那个 callback 有更高的优先级.

error_callback

错误回掉函数, 如果设置了, 那么 Yar 在出错的时候会调用这个回掉函数.

返回值

示例

示例 #1 Yar_Concurrent_Client::loop() 示例

<?php
function callback($retval, $callinfo) {
if (
$callinfo == NULL) {
echo
"现在, 所有的请求都发出去了, 还没有任何请求返回\n";
} else {
echo
"这是一个远程调用的返回, 调用的服务名是 ", $callinfo["method"],
". 调用的 sequence 是 " , $callinfo["sequence"] , "\n";
var_dump($retval);
}
}

function
error_callback($type, $error, $callinfo) {
error_log($error);
}

Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback");
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters")); // if the callback is not specificed,
// callback in loop will be used
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_PACKAGER => "json"));
//this server accept json packager
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_TIMEOUT=>1));
//custom timeout

Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests,
//the error_callback is optional
?>

以上示例的输出类似于:

现在, 所有的请求都发出去了, 还没有任何请求返回
这是一个远程调用的返回, 调用的服务名是 some_method, 调用的 sequence 是 4
string(11) "some_method"
这是一个远程调用的返回, 调用的服务名是 some_method, 调用的 sequence 是 1
string(11) "some_method"
这是一个远程调用的返回, 调用的服务名是 some_method, 调用的 sequence 是 2
string(11) "some_method"
这是一个远程调用的返回, 调用的服务名是 some_method, 调用的 sequence 是 3
string(11) "some_method"

参见

add a note

User Contributed Notes

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