Basic Gearman client and worker, background
GearmanClient
Exemplos
PHP Manual

Exemplo #1 Basic Gearman client and worker, submitting tasks

In this exmple, 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());
}
?>`

O exemplo acima irá imprimir algo similar a:

    % 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

Basic Gearman client and worker, background
GearmanClient
Exemplos
PHP Manual