gpt4 book ai didi

php - Gearman 工作状态的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:42:43 25 4
gpt4 key购买 nike

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

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

最佳答案

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.

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

示例

客户端.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;
}

关于php - Gearman 工作状态的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com