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';
# 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");
# 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); }