Gearman 工作状态的问题 [英] Problem With Gearman Job Status

查看:44
本文介绍了Gearman 工作状态的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

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

<块引用>

向作业发送状态信息服务器和任何侦听客户端.用这指定了作业已完成.

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

示例

client.php

addServer();做{$result = $gmclient->do("linecount", file_get_contents($argv[1]));# 检查各种返回包和错误.switch($gmclient->returnCode()){案例 GEARMAN_WORK_STATUS:list($numerator, $denominator)= $gmclient->doStatus();回声状态:".sprintf("%d%%",($numerator/$denominator)*100)." 完成\r";休息;案例 GEARMAN_SUCCESS:休息;}}while($gmclient->returnCode() != GEARMAN_SUCCESS);echo "\n结果: $result\n";

worker.php

addServer();$worker->addFunction("linecount", "linecount");而 ($worker->work());函数 linecount($job){$lines = preg_split('/[\r\n]/',$job->workload(),null,PREG_SPLIT_NO_EMPTY);$linecount = count($lines);$n = 0;foreach ($lines 作为 $line) {usleep(3000);$n++;$job->sendStatus($n,$linecount);$ret++;}返回 $ret;}

I have a Gearman server running a process which takes a few minutes to finish. I'm running a progress bar to show completion, and am attempting to get the percentages for the bar using the Gearman PHP extension and the jobStatus() function.

The job is definitely active and found, as the first two fields (known + still running) return to true. However the third and fourth fields (numerator and denominator of completion percentage) return with nothing. Does anyone know why this might be or how these numbers are computed?

解决方案

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

Sends status information to the job server and any listening clients. Use this to specify what percentage of the job has been completed.

To be able to use it, you will probably also have alter the client a bit to handle the communication.

Example

client.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";

worker.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;
    }

这篇关于Gearman 工作状态的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆