Friday, December 25, 2009

Using gearman to distribute your work...

Gearman is a system to farm out work to other machines, dispatching function calls to machines that are better suited to do work, to do work in parallel, to load balance lots of function calls, or to call functions between languages.

How does gearman work? Well, a gearman powered app consists of a client, a worker and a job server. The client creats a job and sends it to the job server. The job server finds a suitable worker and sends the job to the worker. Once the job is done, the worker sends the response back to the client via the job server. There are client and worker APIs available for gearman for different languages which allow the app to communicate with the job server.

Too heavy is it... Lets check how this thing actually runs.

Download gearman daemon from

You would need the "Gearman server and library" - the one written in c. And a php extension which we will use to create and communicate with our workers.

I am using gearmand version 0.11, gearman extension for php version 0.60 and php version 5.3 here.

extract the gearmand server and install it using
sudo make install

extract the php extension for gearman and install it using
sudo make install

Enable the extension in php.ini. Add the following line in php.ini

To check if the extension has been enabled run

php -i | grep -i gearman

And you will see something like

gearman support => enabled
libgearman version => 0.11

Now lets write some scripts and check how this works

Create a php client :
# Create our client object.
$client= new GearmanClient();

# Add default server (localhost).

echo "Sending job\n";

# Send reverse job
$result = $client->do("reverse", "Hello World");
if ($result)
echo "Success: $result\n";

Create a php worker :

# Create our worker object.
$worker= new GearmanWorker();

# Add default server (localhost).

# Register function "reverse" with the server.
$worker->addFunction("reverse", "reverse_fn");

while (1)
print "Waiting for job...\n";

$ret= $worker->work();
if ($worker->returnCode() != GEARMAN_SUCCESS)

# A much simple reverse function
function reverse_fn($job)
$workload= $job->workload();
echo "Received job: " . $job->handle() . "\n";
echo "Workload: $workload\n";
$result= strrev($workload);
echo "Result: $result\n";
return $result;

To test the process

start the gearmand server

start the worker
php -q gearmanWorker.php

And send jobs to the worker
php -q gearmanClient.php

The output is as below

1 comment:

Anonymous said...

I am having difficulties installing gearmand version 0.11. I am currently using version 0.6 and it seems to be working fine. The configure runs fine but on running make I get the following error

../libgearman-server/.libs/ undefined reference to `PQescapeStringConn'
collect2: ld returned 1 exit status
make[2]: *** [gearmand] Error 1
make[2]: Leaving directory `/opt/gearmand-0.11/gearmand'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/opt/gearmand-0.11'

Thanks in advance.