我有一台 Gearman 服务器正在运行一个需要几分钟才能完成的进程。我正在运行一个进度条来显示完成情况,并尝试使用 Gearman PHP 扩展和 jobStatus() 函数获取进度条的百分比。
该作业肯定处于活动状态并已找到,因为前两个字段(已知 + 仍在运行)返回 true。然而,第三和第四个字段(完成百分比的分子和分母)什么也没返回。有谁知道为什么会这样或者这些数字是如何计算的?
public bool GearmanJob::sendStatus ( int $numerator , int $denominator )
向作业发送状态信息
服务器和任何侦听客户端。使用
这指定了多少百分比
工作已经完成。
为了能够使用它,您可能还需要对客户端进行一些更改来处理通信。
Example
客户端.php
<?php
global $argc,$argv;
if (!file_exists($argv[1])) {
echo "File not found\n";
exit(1);
}
$gmclient= new GearmanClient();
$gmclient->addServer();
do
{
$result = $gmclient->do("linecount", file_get_contents($argv[1]));
# Check for various return packets and errors.
switch($gmclient->returnCode())
{
case GEARMAN_WORK_STATUS:
list($numerator, $denominator)= $gmclient->doStatus();
echo "Status: " . sprintf("%d%%",($numerator/$denominator)*100)
. " complete\r";
break;
case GEARMAN_SUCCESS:
break;
}
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);
echo "\nResult: $result\n";
工人.php
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("linecount", "linecount");
while ($worker->work());
function linecount($job)
{
$lines = preg_split('/[\r\n]/',
$job->workload(),null,PREG_SPLIT_NO_EMPTY);
$linecount = count($lines);
$n = 0;
foreach ($lines as $line) {
usleep(3000);
$n++;
$job->sendStatus($n,$linecount);
$ret++;
}
return $ret;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)