Gearman工作状态问题

Aar*_*rks 5 php gearman progress-bar

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

这个工作肯定是活跃的,因为前两个字段(已知+仍在运行)返回true.但是,第三和第四个字段(完成百分比的分子和分母)没有返回.有谁知道为什么会这样或这些数字是如何计算的?

Pet*_*ist 3

public bool GearmanJob::sendStatus ( int $numerator , int $denominator )
Run Code Online (Sandbox Code Playgroud)

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

为了能够使用它,您可能还需要对客户端进行一些更改来处理通信。

例子

客户端.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";
Run Code Online (Sandbox Code Playgroud)

工人.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;
    }
Run Code Online (Sandbox Code Playgroud)