©
This document uses PHP Chinese website manual Release
(PECL gearman >= 0.5.0)
GearmanClient::addTask — Add a task to be run in parallel
$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.
function_name
由程序自动执行的已注册函数。
workload
被处理的序列化数据。
context
与任务关联的应用程序上下文。
unique
用于标识特定任务的唯一性 ID。
A GearmanTask object or FALSE
if the task could not be added.
Example #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
Example #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
[#1] liv_romania at yahoo dot com [2015-07-19 12:08:06]
On PHP 5.5 you can use the following code for passing context by reference and avoid "Call-time pass-by-reference has been removed":
<?php
$client = new GearmanClient();
$client->addServer();
# Set a function to be called when the work is complete
$client->setCompleteCallback("reverse_complete");
# Use StdClass instead of array
$results = new StdClass();
$results->value = array();
# Add some tasks for a placeholder of where to put the results
$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->value as $id => $result) {
echo $id . ": " . $result['handle'] . ", " . $result['data'] . "\n";
}
function reverse_complete(GearmanTask $task, StdClass $results)
{
$results->value[$task->unique()] = array(
"handle" => $task->jobHandle(),
"data" => $task->data()
);
}
?>
[#2] stanislav dot reshetnev at gmail dot com [2014-10-31 16:17:09]
Note that param $unique must be different for multiple tasks if You want to run they all separately. If param $unique is equal for multiple tasks You will get the same task:
<?php
$unique=1;
$gclient = GearmanClient();
$gclient->addServer('srv');
$this->setCreatedCallback(function(GearmanTask $task) {
print $task->jobHandle() . "\n";
});
$gclient->addTask('function_name', 'workload', null, $unique);
$gclient->addTask('function_name', 'workload', null, $unique);
$gclient->addTask('function_name', 'workload', null, $unique);
$gclient->runTasks();
sleep(5);
?>
This sript will print only one handler:
H:srv:377382343
H:srv:377382343
H:srv:377382343
[#3] Jeremy Zerr [2013-11-15 18:14:00]
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:
<?php
# 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:
<?php
$results = new \stdClass();
$client->addTask("reverse", "Hello World!", $results, "t1");
?>
Then for completeness, change the complete handler to expect a reference:
<?php
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.