(PECL yar >= 1.0.0)
Yar_Concurrent_Client::loop — 发送所有调用
$callback
= ?, callable $error_callback
= ?): bool发送所有注册的远程 RPC 调用。
callback
如果设置了此回调,则 Yar 会在所有调用发送并且任何响应返回之前调用此回调,且 $callinfo 为 NULL。
然后,如果用户在注册并发调用时没有指定回调,则此回调将用于处理响应,否则将使用注册时指定的回调。
error_callback
如果设置了此回调,则 Yar 会在发生错误时调用此回调。
示例 #1 Yar_Concurrent_Client::loop() 示例
<?php
function callback($retval, $callinfo) {
if ($callinfo == NULL) {
echo "现在,所有请求都已发送,并且没有可用的响应\n";
} else {
echo "这是一个远程调用响应,方法名称是", $callinfo["method"],
". 调用顺序是 " , $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")); // 如果没有指定回调,
// 循环中的回调将被使用
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_PACKAGER => "json"));
// 该服务器接受 json packager
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_TIMEOUT=>1));
// 自定义超时
Yar_Concurrent_Client::loop("callback", "error_callback"); // 发送请求,
// error_callback 是可选的
?>
上面的例子将输出类似于
Now, all requests are sent, and no any response available This is a remote call response, the method name issome_method. calling sequence is 4 string(11) "some_method" This is a remote call response, the method name issome_method. calling sequence is 1 string(11) "some_method" This is a remote call response, the method name issome_method. calling sequence is 2 string(11) "some_method" This is a remote call response, the method name issome_method. calling sequence is 3 string(11) "some_method"