我是php-gearman的新手,在一些有用的教程的帮助下,我尝试了一些例子。像上面的基本代码一样。
客户端
<?php
# Create our client object.
$client= new GearmanClient();
# Add default server (localhost).
$client->addServer("localhost", 4730);
echo "Sending job\n";
# Send reverse job
$result = $client->doBackground("reverse", "Hello!");
$stat = $client->jobStatus($result);
$done = false;
$client->setWorkloadCallback('test');
function test($data)
{
echo $data;
}
while(!$done)
{
if(!$stat[0])
{
$done = true;
echo $result;
}
echo "status".json_encode($stat)."/n";
$stat = $client->jobStatus($result);
}
?>
服务器端
<?php
# Create our worker object.
$worker= new GearmanWorker();
# Add default server (localhost).
$worker->addServer("localhost", 4730);
# Register function "reverse" with the server.
$worker->addFunction("reverse", "reverse_fn");
while (1)
{
print "Waiting for job...\n";
$ret= $worker->work();
if ($worker->returnCode() != GEARMAN_SUCCESS)
break;
}
function reverse_fn($job)
{
$workload= $job->workload();
echo "Received job: " . $job->handle() . "\n";
echo "Workload: $workload\n";
$result= strrev($workload);
echo "Result: $result\n";
return $result;
}
?>
此代码让我获得工作状态。
我的问题是,在使用GermanClient :: doBackground时如何在成功执行工作程序后获取返回的数据?
答案 0 :(得分:0)
虽然这个帖子很古老,但有些人可以使用某种数据库,并将作业句作为一列。然后,您可以在之后查询作业句柄。