(PECL gearman >= 0.5.0)
GearmanClient::addTask — Add a task to be run in parallel
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.
Une fonction enregistrée que le travailleur va exécuter
Données linéarisées à analyser
Contexte de l'application à associer avec une tâche
Un identifiant unique utilisé pour identifier une tâche particulière
A GearmanTask object or FALSE if the task could not be added.
Exemple #1 Basic submission of two tasks
<?php
# Create our gearman client
$gmclient= new GearmanClient();
# add the default job server
$gmclient->addServer();
# 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");
# set a function to be called when the work is complete
$gmclient->setCompleteCallback("complete");
# run the tasks
$gmclient->runTasks();
function complete($task)
{
print "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
COMPLETE: 2, Hello World! COMPLETE: 1, !dlroW olleH
Exemple #2 Basic submission of two tasks with passing application context
<?php
$client = new GearmanClient();
$client->addServer();
# 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->setCompleteCallback("reverse_complete");
$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());
}
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
t2: H.foo:21, Hello World! t1: H:foo:22, !dlroW olleH