Example #1 Basic Gearman client and worker, background
This example shows a very simple client and worker. The client sends a string to the job server as a background job, and the worker reverses the string. Note that since the work is performed asynchronously, the client does not wait for the job to complete and exits (and hence the client never receives the results).
# create our client object
$gmclient= new GearmanClient();
# add the default server (localhost)
# run reverse client in the background
$job_handle = $gmclient->doBackground("reverse", "this is a test");
if ($gmclient->returnCode() != GEARMAN_SUCCESS)
echo "bad return code\n";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
print "Waiting for job...\n";
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
echo "return_code: " . $gmworker->returnCode() . "\n";
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";
echo "Result: $result\n";
# Return what we want to send back to the client.
# A much simpler and less verbose version of the above function would be:
Exemplul de mai sus va afișa ceva similar cu:
% php reverse_worker.php Starting Waiting for job... Received job: H:foo.local:41 Workload: this is a test (14) 1/14 complete 2/14 complete 3/14 complete 4/14 complete 5/14 complete 6/14 complete 7/14 complete 8/14 complete 9/14 complete 10/14 complete 11/14 complete 12/14 complete 13/14 complete 14/14 complete Result: tset a si siht
% php reverse_client_bg.php done!