SunshinePHP Developer Conference 2015


(PECL gearman >= 0.5.0)

GearmanClient::addTaskAdd a task to be run in parallel


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.



O funcție înregistrată pe care trebuie să o execute serverul executor


Datele serializate pentru a fi procesate


Contextul aplicației pentru a fi asociat cu o sarcină


Un ID unical utilizat pentru a identifica o anumită sarcină

Valorile întoarse

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


Example #1 Basic submission of two tasks


# Create our gearman client
$gmclient= new GearmanClient(); 

# add the default job server

# set a function to be called when the work is 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


"COMPLETE: " $task->unique() . ", " $task->data() . "\n"


Exemplul de mai sus va afișa ceva similar cu:

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

Example #2 Basic submission of two tasks with passing application context


= new GearmanClient();

# set a function to be called when the work is 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");


# The results should now be filled in from the callbacks
foreach ($results as $id => $result)
$id ": " $result['handle'] . ", " $result['data'] . "\n";

$results[$task->unique()] = array("handle"=>$task->jobHandle(), "data"=>$task->data());


Exemplul de mai sus va afișa ceva similar cu:

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

Vedeți de asemenea

add a note add a note

User Contributed Notes 1 note

Jeremy Zerr
11 months ago
As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in $client->addTask(..., ..., &$results, ...);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

So that means that when you call addTask with a context parameter as in the example above like this:

# Add some tasks for a placeholder of where to put the results
$results = array();
$client->addTask("reverse", "Hello World!", &$results, "t1");

You get this "call-time pass-by-reference" warning (or error).  This can be avoided and still result in functional code by changing the context variable to be an object so that it is passed by reference like this:

= new \stdClass();
$client->addTask("reverse", "Hello World!", $results, "t1");

Then for completeness, change the complete handler to expect a reference:

function reverse_complete($task, &$results) { ... }

Then inside the complete handler, you can use the $results object to save your results to be accessible outside the complete handler.
To Top