5

我有一个 Gearman 服务器正在运行一个需要几分钟才能完成的进程。我正在运行一个进度条来显示完成情况,并尝试使用 Gearman PHP 扩展和 jobStatus() 函数获取进度条的百分比。

由于前两个字段(已知 + 仍在运行)返回 true,因此该作业肯定处于活动状态并已找到。然而,第三个和第四个字段(完成百分比的分子和分母)什么都没有返回。有谁知道为什么会这样或这些数字是如何计算的?

4

2 回答 2

3
public bool GearmanJob::sendStatus ( int $numerator , int $denominator )

将状态信息发送到作业服务器和任何侦听客户端。使用它来指定已完成作业的百分比。

为了能够使用它,您可能还需要稍微改变客户端来处理通信。

例子

客户端.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;
    }
于 2011-04-15T08:24:27.970 回答
0

工人是否配置为返回状态?

如果您自己编写它们,则必须做一些额外的工作才能让它们在执行过程中返回详细信息。

于 2011-04-14T16:56:33.897 回答