GearmanClient::addTaskBackground
GearmanClient::addTaskBackground
(PECL gearman >= 0.5.0)
GearmanClient :: addTaskBackground - 添加一个后台任务并行运行
描述
public GearmanTask GearmanClient::addTaskBackground ( string $function_name , string $workload [, mixed &$context [, string $unique ]] )
添加要与其他任务并行运行的后台任务。为所有要并行运行的任务调用此方法,然后调用GearmanClient :: runTasks()来执行该工作。
参数
function_name
工作人员执行的注册功能
workload
要处理的序列化数据
context
应用程序上下文与任务相关联
unique
用于识别特定任务的唯一ID
返回值
GearmanTask对象或FALSE
无法添加任务。
例子
例#1两个任务,一个背景,一个不是
这个例子说明了运行一个后台任务和一个普通任务的区别。客户端添加两个任务来执行相同的功能,但添加了addTaskBackground()
。回调被设置为可以追踪作业的进度。一个简单的人工延迟报告工作进度,客户通过回调选择。这个例子运行两名工人。请注意后台任务不显示在客户端输出中。
<?php
# The client script
# create our gearman client
$gmc= new GearmanClient(
# add the default job server
$gmc->addServer(
# set a couple of callbacks so we can track progress
$gmc->setCompleteCallback("reverse_complete"
$gmc->setStatusCallback("reverse_status"
# add a task for the "reverse" function
$task= $gmc->addTask("reverse", "Hello World!", null, "1"
# add another task, but this one to run in the background
$task= $gmc->addTaskBackground("reverse", "!dlroW olleH", null, "2"
if (! $gmc->runTasks())
{
echo "ERROR " . $gmc->error() . "\n";
exit;
}
echo "DONE\n";
function reverse_status($task)
{
echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() .
"/" . $task->taskDenominator() . "\n";
}
function reverse_complete($task)
{
echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}
?>
<?php
# The worker script
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;
}
?>
两名工人的工作量输出:
Received job: H:foo.local:65
Workload: !dlroW olleH (12)
1/12 complete
Received job: H:foo.local:66
Workload: Hello World! (12)
Sending status: 1/12 complete
Sending status: 2/12 complete
Sending status: 2/12 complete
Sending status: 3/12 complete
Sending status: 3/12 complete
Sending status: 4/12 complete
Sending status: 4/12 complete
Sending status: 5/12 complete
Sending status: 5/12 complete
Sending status: 6/12 complete
Sending status: 6/12 complete
Sending status: 7/12 complete
Sending status: 7/12 complete
Sending status: 8/12 complete
Sending status: 8/12 complete
Sending status: 9/12 complete
Sending status: 9/12 complete
Sending status: 10/12 complete
Sending status: 10/12 complete
Sending status: 11/12 complete
Sending status: 11/12 complete
Sending status: 12/12 complete
Sending status: 12/12 complete
Result: !dlroW olleH
Result: Hello World!
客户端输出:
STATUS: 1, H:foo.local:66 - 1/12
STATUS: 1, H:foo.local:66 - 2/12
STATUS: 1, H:foo.local:66 - 3/12
STATUS: 1, H:foo.local:66 - 4/12
STATUS: 1, H:foo.local:66 - 5/12
STATUS: 1, H:foo.local:66 - 6/12
STATUS: 1, H:foo.local:66 - 7/12
STATUS: 1, H:foo.local:66 - 8/12
STATUS: 1, H:foo.local:66 - 9/12
STATUS: 1, H:foo.local:66 - 10/12
STATUS: 1, H:foo.local:66 - 11/12
STATUS: 1, H:foo.local:66 - 12/12
COMPLETE: 1, !dlroW olleH
DONE