array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), 'this' => array ( 0 => 'gearmanclient.addtask.php', 1 => 'GearmanClient::addTask', ), 'up' => array ( 0 => 'class.gearmanclient.php', 1 => 'GearmanClient', ), 'prev' => array ( 0 => 'gearmanclient.addservers.php', 1 => 'GearmanClient::addServers', ), 'next' => array ( 0 => 'gearmanclient.addtaskbackground.php', 1 => 'GearmanClient::addTaskBackground', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/gearman/gearmanclient/addtask.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PECL gearman >= 0.5.0)
GearmanClient::addTask — Add a task to be run in parallel
$function_name
,$workload
,$context
= null
,$unique_key
= null
Adds a task to be run in parallel with other tasks. Call this method for all the tasks to be run in parallel, then call GearmanClient::runTasks() to perform the work. Note that enough workers need to be available for the tasks to all run in parallel.
function_name
ワーカーが実行するために登録した関数。
workload
シリアライズしたデータ。
context
タスクに関連づけるアプリケーションコンテキスト。
unique_key
タスクを特定するために用いる一意な ID。
A GearmanTask object or false
if the task could not be added.
例1 Basic submission of two tasks
<?php
# Create our gearman client
$gmclient= new GearmanClient();
# add the default job server
$gmclient->addServer();
# set a function to be called when the work is complete
$gmclient->setCompleteCallback("complete");
# add a task to perform the "reverse" function on the string "Hello World!"
$gmclient->addTask("reverse", "Hello World!", null, "1");
# add another task to perform the "reverse" function on the string "!dlroW olleH"
$gmclient->addTask("reverse", "!dlroW olleH", null, "2");
# run the tasks
$gmclient->runTasks();
function complete($task)
{
print "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}
?>
上の例の出力は、 たとえば以下のようになります。
COMPLETE: 2, Hello World! COMPLETE: 1, !dlroW olleH
例2 Basic submission of two tasks with passing application context
<?php
$client = new GearmanClient();
$client->addServer();
# set a function to be called when the work is complete
$client->setCompleteCallback("reverse_complete");
# Add some tasks for a placeholder of where to put the results
$results = array();
$client->addTask("reverse", "Hello World!", $results, "t1");
$client->addTask("reverse", "!dlroW olleH", $results, "t2");
$client->runTasks();
# The results should now be filled in from the callbacks
foreach ($results as $id => $result)
echo $id . ": " . $result['handle'] . ", " . $result['data'] . "\n";
function reverse_complete($task, $results)
{
$results[$task->unique()] = array("handle"=>$task->jobHandle(), "data"=>$task->data());
}
?>
上の例の出力は、 たとえば以下のようになります。
t2: H.foo:21, Hello World! t1: H:foo:22, !dlroW olleH