Basic Gearman client and worker, submitting tasks
Example #1 Basic Gearman client and worker, submitting tasks
In this example, the basic reverse client extended to run two tasks in parallel. The reverse worker is unchanged except to add sending of data back during processing.
<?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";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
$gmworker->addServer();
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$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";
# This status loop is not needed, just showing how it works
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";
# Return what we want to send back to the client.
return $result;
}
# A much simpler and less verbose version of the above function would be:
function reverse_fn_fast($job)
{
return strrev($job->workload());
}
?>
The above example will output something similar to:
% 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
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-gearman.examples-reverse-task.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.