GearmanClient::addTaskStatus
GearmanClient::addTaskStatus
(PECL gearman >= 0.5.0)
GearmanClient :: addTaskStatus - 添加一个任务来获取状态
描述
public GearmanTask GearmanClient::addTaskStatus ( string $job_handle [, string &$context ] )
用于从Gearman服务器请求状态信息,该服务器将调用指定的状态回调(使用GearmanClient :: setStatusCallback()进行设置)。
参数
job_handle
任务获取状态的作业句柄
context
要传递给状态回调的数据,通常是对数组或对象的引用
返回值
GearmanTask对象。
例子
示例#1监视多个后台任务的完成情况
在这个例子中,员工引入了一个虚拟的延迟来模拟一个长时间运行的过程。这个例子只有一名员工在运行。
<?php
/* create our object */
$gmclient= new GearmanClient(
/* add the default server */
$gmclient->addServer(
/* start some background jobs and save the handles */
$handles = array(
$handles[0] = $gmclient->doBackground("reverse", "Hello World!"
$handles[1] = $gmclient->doBackground("reverse", "!dlroW olleH"
$gmclient->setStatusCallback("reverse_status"
/* Poll the server to see when those background jobs finish; */
/* a better method would be to use event callbacks */
do
{
/* Use the context variable to track how many tasks have completed */
$done = 0;
$gmclient->addTaskStatus($handles[0], &$done
$gmclient->addTaskStatus($handles[1], &$done
$gmclient->runTasks(
echo "Done: $done\n";
sleep(1
}
while ($done != 2
function reverse_status($task, $done)
{
if (!$task->isKnown())
$done++;
}
?>
上面的例子会输出类似于:
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 0
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 1
Done: 2