downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

GearmanClient::addTaskBackground> <GearmanClient::addServers
[edit] Last updated: Fri, 17 May 2013

view this page in

GearmanClient::addTask

(PECL gearman >= 0.5.0)

GearmanClient::addTaskAdd a task to be run in parallel

Beschreibung

public GearmanTask GearmanClient::addTask ( string $function_name , string $workload [, mixed &$context [, string $unique ]] )

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.

Parameter-Liste

function_name

Die registrierte Funktion, die der Worker ausführen soll

workload

Serialisierte Daten, die verarbeitet werden sollen

context

Der Anwendungskontext der mit einem Task verknüpft werden soll

unique

Eine eindeutige ID, die einen bestimmten Task identifiziert

Rückgabewerte

A GearmanTask object or FALSE if the task could not be added.

Beispiele

Beispiel #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"
}

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

COMPLETE: 2, Hello World!
COMPLETE: 1, !dlroW olleH

Beispiel #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());
}

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

t2: H.foo:21, Hello World!
t1: H:foo:22, !dlroW olleH

Siehe auch



add a note add a note User Contributed Notes GearmanClient::addTask - [1 notes]
up
0
sfinktah at php dot spamtrak dot org
2 years ago
To create a matching worker, capable of executing multiple jobs simultaneously, I came up with this.   It's a slight hack, but it works.

<?php
do {
  
$pid = false;
  
  
$gmw= new GearmanWorker();
  
$gmw->addServer("");
  
$gmw->addFunction("lookup7", "lookup7_fast", $args);
   @
$gmw->work();
} while (
$pid);

function
lookup7_fast($job) {
   global
$pid;
  
   if (
$pid = pcntl_fork()) {
      return
$job->setReturn(GEARMAN_WORK_STATUS);
   }
   
   return
do_work($job->workload());
};
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites