GearmanClient::addTask

(PECL gearman >= 0.5.0)

GearmanClient::addTaskAñade una tarea para ser ejecutada en paralelo

Descripción

public GearmanClient::addTask(
    string $function_name,
    string|int|float $workload,
    mixed $context = null,
    ?string $unique_key = null
): GearmanTask|false

Añade una tarea para ser ejecutada en paralelo con otras tareas. Esta método debe ser llamado para todas las tareas a ejecutar en paralelo, y posteriormente, debe llamarse al método GearmanClient::runTasks() para ejecutar las tareas. Tenga en cuenta que es necesario contar con suficientes agentes disponibles para ejecutar en paralelo todas las tareas.

Parámetros

function_name

Función registrada que ejecutará el worker

workload

Datos serializados a ser procesados

context

Aplicación context a asociar con la tarea

unique_key

ID único para identificar una tarea en particular

Valores devueltos

Un objeto GearmanTask o false si la tarea no pudo ser añadida.

Ejemplos

Ejemplo #1 Añadir 2 tareas

<?php

# Crea un cliente gearman
$gmclient= new GearmanClient();

# Añade el servidor de trabajos por defecto
$gmclient->addServer();

# Define una función a llamar cuando el trabajo está completo
$gmclient->setCompleteCallback("complete");

# Añade una tarea que ejecuta la función "reverse" sobre la cadena "Hello World!"
$gmclient->addTask("reverse", "Hello World!", null, "1");

# Añade otra tarea que ejecuta la función "reverse" sobre la cadena "!dlroW olleH"
$gmclient->addTask("reverse", "!dlroW olleH", null, "2");

# Ejecuta las tareas
$gmclient->runTasks();

function
complete($task)
{
print
"Completado : " . $task->unique() . ", " . $task->data() . "\n";
}

?>

El resultado del ejemplo sería algo similar a:

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

Ejemplo #2 Añadir 2 tareas pasando el contexto de la aplicación

<?php

$client
= new GearmanClient();
$client->addServer();

# Define una función a llamar cuando el trabajo está completo
$client->setCompleteCallback("reverse_complete");

# Añade algunas tareas que contienen un marcador en el lugar donde debe colocarse el resultado
$results = array();
$client->addTask("reverse", "Hello World!", $results, "t1");
$client->addTask("reverse", "!dlroW olleH", $results, "t2");

$client->runTasks();

# El resultado debe estar ahora contenido en las funciones de devolución de llamada
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());
}

?>

El resultado del ejemplo sería algo similar a:

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

Ver también

add a note

User Contributed Notes 3 notes

up
7
liv_romania at yahoo dot com
9 years ago
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()
);
}
?>
up
1
stanislav dot reshetnev at gmail dot com
10 years ago
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
up
0
Jeremy Zerr
11 years 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:

<?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.
To Top