• Tweet

Do you develop web applications? The web apps you build, do they require background processing of tasks? Then you might be interested in this tool. Gearman is a job server. If we quote from Wikipedia:

Gearman is an open source application framework originally written in Perl by Brad Fitzpatrick. Brian Aker and Eric Day rewrote the framework in C. Gearman is designed to distribute appropriate computer tasks to multiple computers, so large tasks can be done more quickly. In some cases, load balancing rather than raw speed may be the main goal; a Web server, for instance, could use Gearman to send tasks for which it is not optimized to another computer (which may be running on a different architecture, using another operating system, or loaded with a computer language better suited to a particular operation).

As you can see, Gearman is a daemon like process. It runs in the background, it lets worker processes register themselves and distribute workloads among the workers when clients deliver the payloads.

So, what could be a real life use case? Suppose, you allow your users to mass photo uploads. You then need to resize them. Obviously, this is a tedious task and you can’t do it in real time. What you should do is to process the task in background. Gearman lets you do that in fashion. You shall need to create a function that processes the images and assign the function as a worker. Now, when the upload is done, the web app can call the background process to finish the task silently.

Installation is pretty simple. Execute the following commands on your terminal:

sudo apt-get install gearman

After installation, run the daemon using:

sudo gearmand -d -u root

If you want to debug workers and want verbose output, please use this:

sudo gearmand -d -vvv -u root

Once the server is started, you can register workers and use clients to invoke the workers. In future posts, we shall see how we can use Gearman from PHP, Python and other languages.