Gearman 基本客户端和工作程序,提交任务

示例 #1 Gearman 基本客户端和工作程序,提交任务

在本示例中,基本反转客户端扩展为并行运行两个任务。反转工作程序保持不变,只是增加了在处理期间发送数据的功能。

<?php

# 创建 Gearman 客户端
$gmc= new GearmanClient();

# 添加默认服务器 (localhost)
$gmc->addServer();

# 注册一些回调函数
$gmc->setCreatedCallback("reverse_created");
$gmc->setDataCallback("reverse_data");
$gmc->setStatusCallback("reverse_status");
$gmc->setCompleteCallback("reverse_complete");
$gmc->setFailCallback("reverse_fail");

# 设置一些任意的应用程序数据
$data['foo'] = 'bar';

# 添加两个任务
$task= $gmc->addTask("reverse", "foo", $data);
$task2= $gmc->addTaskLow("reverse", "bar", NULL);

# 并行运行任务 (假设有多个工作程序)
if (! $gmc->runTasks())
{
echo
"ERROR " . $gmc->error() . "\n";
exit;
}

echo
"DONE\n";

function
reverse_created($task)
{
echo
"CREATED: " . $task->jobHandle() . "\n";
}

function
reverse_status($task)
{
echo
"STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() .
"/" . $task->taskDenominator() . "\n";
}

function
reverse_complete($task)
{
echo
"COMPLETE: " . $task->jobHandle() . ", " . $task->data() . "\n";
}

function
reverse_fail($task)
{
echo
"FAILED: " . $task->jobHandle() . "\n";
}

function
reverse_data($task)
{
echo
"DATA: " . $task->data() . "\n";
}

?>
<?php

echo "开始\n";

# 创建工作者对象。
$gmworker= new GearmanWorker();

# 添加默认服务器 (localhost)。
$gmworker->addServer();

# 向服务器注册函数 "reverse"。将工作者函数改为
# "reverse_fn_fast" 以获得速度更快的无输出工作者。
$gmworker->addFunction("reverse", "reverse_fn");

print
"等待任务...\n";
while(
$gmworker->work())
{
if (
$gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo
"返回码: " . $gmworker->returnCode() . "\n";
break;
}
}

function
reverse_fn($job)
{
echo
"收到任务: " . $job->handle() . "\n";

$workload = $job->workload();
$workload_size = $job->workloadSize();

echo
"工作负载: $workload ($workload_size)\n";

# 此状态循环不需要,只是展示了它的工作原理
for ($x= 0; $x < $workload_size; $x++)
{
echo
"发送状态: " . ($x + 1) . "/$workload_size 完成\n";
$job->sendStatus($x+1, $workload_size);
$job->sendData(substr($workload, $x, 1));
sleep(1);
}

$result= strrev($workload);
echo
"结果: $result\n";

# 返回要发送回客户端的内容。
return $result;
}

# 以上函数的更简单、更简洁的版本为:
function reverse_fn_fast($job)
{
return
strrev($job->workload());
}

?>

上面的示例将输出类似于以下内容

% php reverse_worker.php
Starting
Waiting for job...
Received job: H:foo.local:45
Workload: foo (3)
1/3 complete
2/3 complete
3/3 complete
Result: oof
Received job: H:foo.local:44
Workload: bar (3)
1/3 complete
2/3 complete
3/3 complete
Result: rab
% php reverse_client_task.php
CREATED: H:foo.local:44
CREATED: H:foo.local:45
STATUS: H:foo.local:45 - 1/3
DATA: f
STATUS: H:foo.local:45 - 2/3
DATA: o
STATUS: H:foo.local:45 - 3/3
DATA: o
COMPLETE: H:foo.local:45, oof
STATUS: H:foo.local:44 - 1/3
DATA: b
STATUS: H:foo.local:44 - 2/3
DATA: a
STATUS: H:foo.local:44 - 3/3
DATA: r
COMPLETE: H:foo.local:44, rab
DONE

添加说明

用户贡献的说明 1 条说明

septerrianin at mail dot ru
5 年前
注意。
回调函数的注册需要在添加任务之前进行。

此示例无法正常工作
<?php
# 添加任务
$task= $gmc->addTask("reverse", "foo", $data);

# 注册一些回调函数
$gmc->setCreatedCallback("reverse_created");
$gmc->setDataCallback("reverse_data");
$gmc->setStatusCallback("reverse_status");
$gmc->setCompleteCallback("reverse_complete");
$gmc->setFailCallback("reverse_fail");

# 运行任务
$gmc->runTasks();
?>
To Top