GearmanClient::doNormal
GearmanClient::doNormal
(没有可用的版本信息,可能只在Git中)
GearmanClient :: doNormal - 运行一个任务并返回一个结果
描述
public string GearmanClient::doNormal ( string $function_name , string $workload [, string $unique ] )
运行单个任务并返回结果的字符串表示形式。由GearmanClient和GearmanWorker决定结果的格式。
参数
function_name
工作人员执行的注册功能
workload
要处理的序列化数据
unique
用于识别特定任务的唯一ID
返回值
表示运行任务结果的字符串。
例子
示例#1简单的任务提交并立即返回
<?php
?>
<?php
# Client code
echo "Starting\n";
# Create our client object.
$gmclient= new GearmanClient(
# Add default server (localhost).
$gmclient->addServer(
echo "Sending job\n";
$result = $gmclient->doNormal("reverse", "Hello!"
echo "Success: $result\n";
?>
<?php
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker(
# Add default server (localhost).
$gmworker->addServer(
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn"
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job)
{
return strrev($job->workload()
}
?>
上面的例子会输出类似于:
Starting
Sending job
Success: !olleH
示例#2提交作业并检索增量状态
提交作业并且脚本循环以检索状态信息。工作人员有一个人工延迟,导致长时间运行的作业,并在处理发生时发送状态和数据。随后每次调用GearmanClient :: doNormal()都会
生成正在运行的作业的状态信息。
<?php
# Client code
# Create our client object.
$gmclient= new GearmanClient(
# Add default server (localhost).
$gmclient->addServer(
echo "Sending job\n";
# Send reverse job
do
{
$result = $gmclient->doNormal("reverse", "Hello!"
# Check for various return packets and errors.
switch($gmclient->returnCode())
{
case GEARMAN_WORK_DATA:
echo "Data: $result\n";
break;
case GEARMAN_WORK_STATUS:
list($numerator, $denominator)= $gmclient->doStatus(
echo "Status: $numerator/$denominator complete\n";
break;
case GEARMAN_WORK_FAIL:
echo "Failed\n";
exit;
case GEARMAN_SUCCESS:
break;
default:
echo "RET: " . $gmclient->returnCode() . "\n";
echo "Error: " . $gmclient->error() . "\n";
echo "Errno: " . $gmclient->getErrno() . "\n";
exit;
}
}
while($gmclient->returnCode() != GEARMAN_SUCCESS
echo "Success: $result\n";
?>
<?php
# Worker code
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker(
# Add default server (localhost).
$gmworker->addServer(
# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn"
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job)
{
echo "Received job: " . $job->handle() . "\n";
$workload = $job->workload(
$workload_size = $job->workloadSize(
echo "Workload: $workload ($workload_size)\n";
# This status loop is not needed, just showing how it works
for ($x= 0; $x < $workload_size; $x++)
{
echo "Sending status: " + $x + 1 . "/$workload_size complete\n";
$job->sendStatus($x+1, $workload_size
$job->sendData(substr($workload, $x, 1)
sleep(1
}
$result= strrev($workload
echo "Result: $result\n";
# Return what we want to send back to the client.
return $result;
}
?>
上面的例子会输出:
执行端输出:
Starting
Waiting for job...
Received job: H:foo.local:106
Workload: Hello! (6)
1/6 complete
2/6 complete
3/6 complete
4/6 complete
5/6 complete
6/6 complete
Result: !olleH
客户端输出:
Starting
Sending job
Status: 1/6 complete
Data: H
Status: 2/6 complete
Data: e
Status: 3/6 complete
Data: l
Status: 4/6 complete
Data: l
Status: 5/6 complete
Data: o
Status: 6/6 complete
Data: !
Success: !olleH
扩展内容
- GearmanClient :: doHigh() - 运行一个高优先级的任务
- GearmanClient :: doLow() - 运行一个低优先级的任务
- GearmanClient :: doBackground() - 在后台运行一个任务
- GearmanClient :: doHighBackground() - 在后台运行一个高优先级的任务
- GearmanClient :: doLowBackground() - 在后台运行一个低优先级的任务
← GearmanClient::doLowBackground
GearmanClient::doStatus →