PHP 8.4.1 Released!

Client et agent Gearman simple, avec la soumission de tâches

Exemple #1 Client et agent Gearman simple, avec la soumission de tâches

Dans cet exemple, le client "reverse" a été étendu pour exécuter 2 tâches en parallèle. L'agent "reverse" est inchangé, mis à part qu'il retourne des données durant le processus.

<?php

# create the gearman client
$gmc= new GearmanClient();

# add the default server (localhost)
$gmc->addServer();

# register some callbacks
$gmc->setCreatedCallback("reverse_created");
$gmc->setDataCallback("reverse_data");
$gmc->setStatusCallback("reverse_status");
$gmc->setCompleteCallback("reverse_complete");
$gmc->setFailCallback("reverse_fail");

# set some arbitrary application data
$data['foo'] = 'bar';

# add two tasks
$task= $gmc->addTask("reverse", "foo", $data);
$task2= $gmc->addTaskLow("reverse", "bar", NULL);

# run the tasks in parallel (assuming multiple workers)
if (! $gmc->runTasks())
{
echo
"ERROR " . $gmc->error() . "\n";
exit;
}

echo
"DONE\n";

function
reverse_created($task)
{
echo
"CREATED: " . $task->jobHandle() . "\n";
}

function
reverse_status($task)
{
echo
"STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() .
"/" . $task->taskDenominator() . "\n";
}

function
reverse_complete($task)
{
echo
"COMPLETE: " . $task->jobHandle() . ", " . $task->data() . "\n";
}

function
reverse_fail($task)
{
echo
"FAILED: " . $task->jobHandle() . "\n";
}

function
reverse_data($task)
{
echo
"DATA: " . $task->data() . "\n";
}

?>
<?php

echo "Starting\n";

# Crée un nouvel agent.
$gmworker= new GearmanWorker();

# Ajoute un serveur par défaut (localhost).
$gmworker->addServer();

# Enregistre la fonction "reverse" avecle serveur. Modifie la fonction de l'agent en
# "reverse_fn_fast" pour être plus rapide sans affichage.
$gmworker->addFunction("reverse", "reverse_fn");

print
"Waiting for job...\n";
while(
$gmworker->work())
{
if (
$gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo
"return_code: " . $gmworker->returnCode() . "\n";
break;
}
}

function
reverse_fn($job)
{
echo
"Received job: " . $job->handle() . "\n";

$workload = $job->workload();
$workload_size = $job->workloadSize();

echo
"Workload: $workload ($workload_size)\n";

# Cette boucle n'est pas nécessaire, mais nous l'utilisons pour en comprendre le fonctionnement
for ($x= 0; $x < $workload_size; $x++)
{
echo
"Sending status: " . ($x + 1) . "/$workload_size complete\n";
$job->sendStatus($x+1, $workload_size);
$job->sendData(substr($workload, $x, 1));
sleep(1);
}

$result= strrev($workload);
echo
"Result: $result\n";

# Retourne ce que l'on veut au client.
return $result;
}

# Une version plus simple et moins vebeuse de la fonction ci-dessus serait :
function reverse_fn_fast($job)
{
return
strrev($job->workload());
}

?>

Résultat de l'exemple ci-dessus est similaire à :

% php reverse_worker.php
Starting
Waiting for job...
Received job: H:foo.local:45
Workload: foo (3)
1/3 complete
2/3 complete
3/3 complete
Result: oof
Received job: H:foo.local:44
Workload: bar (3)
1/3 complete
2/3 complete
3/3 complete
Result: rab
% php reverse_client_task.php
CREATED: H:foo.local:44
CREATED: H:foo.local:45
STATUS: H:foo.local:45 - 1/3
DATA: f
STATUS: H:foo.local:45 - 2/3
DATA: o
STATUS: H:foo.local:45 - 3/3
DATA: o
COMPLETE: H:foo.local:45, oof
STATUS: H:foo.local:44 - 1/3
DATA: b
STATUS: H:foo.local:44 - 2/3
DATA: a
STATUS: H:foo.local:44 - 3/3
DATA: r
COMPLETE: H:foo.local:44, rab
DONE

add a note

User Contributed Notes 1 note

up
1
septerrianin at mail dot ru
6 years ago
NOTE.
The register of the callback functions needs BEFORE add a task.

This example NOT working correctly:
<?php
# add tasks
$task= $gmc->addTask("reverse", "foo", $data);

# register some callbacks
$gmc->setCreatedCallback("reverse_created");
$gmc->setDataCallback("reverse_data");
$gmc->setStatusCallback("reverse_status");
$gmc->setCompleteCallback("reverse_complete");
$gmc->setFailCallback("reverse_fail");

# run the task
$gmc->runTasks();
?>
To Top