Other posts in this series
Now that Gearmand is setup we need to get some workers going. Gearman is itself language agnostic; it doesn’t really care if your client/worker is in Python, PHP or Ruby, to name a few. There are several libraries available to help you get going.
##PECL::Gearman In order to get clients and workers setup using PHP you need to have libgearman installed; this came with Gearmand from my previous post in this series. For this example I’ll be using the Gearman PECL extension. I assume you already have PHP installed on your system.
By default, the configure script looks in
to find the libgearman header files. If
you’re using Homebrew, MacPorts, or installed from source you should have no problem.
To install using PECL, run
is required since there is no full fledged stable release to use at this time.
Side Note: This should work fine for installations of libgearman I covered previously, but if you’re like myself and have your Homebrew installed elsewhere (I use Cinderella), your header files are in a different place, and you’ll need to compile the extension by downloading the source, extracting it, and running the following
1 2 3 4 5 6 7 ~$ cd Downloads/gearman-0.7.0/gearman-0.7.0 ~$ phpize ~$ ./configure --with-gearman=`brew --prefix` ~$ make ~$ (sudo) make install Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20090626/ ~$
When the installation completes, you should receive instructions on activating the extension. They typically involve updating
file to include the
near the end of the file. After that,
restart your sever.
To confirm everything is OK, create a simple php file that echo’s out the gearman version:
And run the file
We’ll start with a very simple “reverse string” job to get going. First we’ll create the client to submit the job. We do so by instantiating a new
set the server where Gearmand is running, and calling
with the job name and the workload:
You can run this on the command line like so:
The client will wait for the response (there are other types of jobs where the client does not wait, called background jobs).
Now we create a worker in a similar process: instantiating a new
object and set the server where Gearmand is running, except now we register a method of work
that we can do, and define that function:
Now we run the worker from another terminal session. Because we’ve put the worker in an endless
loop, the worker
will connect to the server and perform jobs until we kill the process with
Back on the original terminal session, where we left the client waiting, you should see an update:
The Gearmand server held on to the job until a worker arrived that could preform the work. Once our worker fired up, the server handed the job over, the worker did the work, and told the server it was done and could accept more work. The server in turn passed the result back to the client, and the client process terminated, printing the reversed string.