Introduction to Gearman – Multitasking in PHP

Share this article

How many times have you developed a web application that had some functionality which would benefit from running an external program or even forking a separate process? This is not something you generally like to do from your web app because you want to make it run as fast and efficient as possible, while keeping the site functional for end users. So how do we get a fast but full-featured application that can process more than the average app we’re used to?

Years ago developers of a web site were facing a similar problem. Users were uploading too many pictures, all of which had to be processed into their respective accounts. This included resizing functionality such as creating thumbnails or reducing the raw image size to something more appropriate for the web site to display. These pictures mostly consisted of avatars, millions of them overloading the app. The team thought of a solution: have a server that spawns processes that do things such as processing pictures, outside of the web server application. This resulted in Gearman.

What is Gearman?

Gearman provides a distributed application framework for work with multiple machines or processes. It allows applications to complete tasks in parallel, to load balance processing and to call functions between languages. The framework can be used in a variety of applications. Gearman is multi-threaded and is known to be able to carry out 50 thousand jobs per-second. Some of the well-known sites using the C version of Gearman are: – Digg: 45+ servers, 400 thousand jobs a day – Yahoo: 120+ servers, 12 million jobs a day

Gearman was originally written in Perl, but the job servers and client API were recently rewritten in C by Eric Day because he wanted better performance.

The figure describes the type of setup you might use for image resizing. Traditionally, the image resizing would have been implemented completely within the web application. The user would upload an image, and within the HTTP request to serve the page, PHP would have to run the image conversion to perform the resizing. The page load would not be completed until the image resizing was complete. Now, with Gearman, the web app can request an image resizing by way of a Gearman client to the Gearman job server. Gearman allows you to separate some functionality from your web application letting other parts of your environment take care of it.

Installing and Running Gearman

Installing it is easy and straightforward if you just follow their comprehensive tutorial, assuming you have a high-level understanding of roles like a job server, clients, and workers; if you run into any problems while trying to install it, let us know in the comments below.

Gearman and PHP

Using Gearman with PHP makes for an ideal easy-to-use combination. You have a Gearman client, which is usually your application and is the code that sends jobs to the Gearman job server. You also have the worker component, which uses the PHP Gearman worker library to register itself as handling a named job and then specifies the function name for the job. The PHP extension extends the procedural interface to provide a native object oriented interface as well.

To install the Gearman extension into PHP, refer to your OS documentation. On Ubuntu and Linux Mint, for example, it’s as easy as sudo apt-get install php5-gearman. On some systems, however, you might have to manually build it and include it into your PHP extensions folder (you can find out where it is by looking at your phpinfo()) and then include it in the php.ini file like so: extension="gearman.so".

To check if Gearman is successfully installed, see your phpinfo() again or just run a test method:

<?php
var_dump( gearman_version() );
?>

Let’s look at an example:

<?php
$client= new GearmanClient();

$client->addServer(‘127.0.0.1’);

$client->setCreatedCallback("create_change");

$client->setDataCallback("data_change");

$client->setStatusCallback("status_change");

$client->setCompleteCallback("complete_change");

$client->setFailCallback("fail_change");

$data_array =array('mydata'=>’task’);

$task= $client->addTask("reverse", "mydata", $data_array);

$task2= $client->addTaskLow("reverse", "task", NULL);

echo "DONE\n";

function create_change($task)
{
    echo "CREATED: " . $task->jobHandle() . "\n";
}

function status_change($task)
{
    echo "STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() . 
         "/" . $task->taskDenominator() . "\n";
}

function complete_change($task)
{
    echo "COMPLETE: " . $task->jobHandle() . ", " . $task->data() . "\n";
}

function fail_change($task)
{
    echo "FAILED: " . $task->jobHandle() . "\n";
}

function data_change($task)
{
    echo "DATA: " . $task->data() . "\n";
}
Function Client_error()
{
if (! $client->runTasks())
    return $client->error() ;
}

?>

In this client example, a GearmanClient object is instantiated. Next, the Gearman client API addServer() method is called to add a server to be used for the client connection. Multiple servers could be added if desired. An empty argument would default to localhost – it was explicitly specified here for the sake of clarity.

Next, we set up some callbacks for various stages of the tasks – the concept of callbacks should be familiar to every intermediate developer.

addTask adds a task to be run in parallel with other tasks. addTaskLow adds a low priority background task to be run in parallel with other tasks. To perform the work, we call GearmanClient::runTasks(). Note that enough workers need to be available for the tasks to run in parallel. Tasks with a lower priority will be chosen from the queue after those of higher priority.

Conclusion

In this short introduction into Gearman, you learned about multitasking with PHP applications. You can now implement certain functionality external to your web application and achieve better performance while leaving little to none of the rest of your system idle.

In a future article, we’ll cover a detailed real world use case of Gearman with a working demo. For now, please don’t hesitate to leave a comment below if you’d like anything covered in more detail.

Frequently Asked Questions (FAQs) about Gearman and Multi-tasking in PHP

What is Gearman and why is it important in PHP?

Gearman is an open-source application framework designed for distributing work to multiple machines or processes. It is important in PHP because it allows developers to easily create scalable and distributed applications. Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages.

How does Gearman work in PHP?

Gearman works in PHP by allowing developers to create a system where a client program can offload tasks to worker programs. The client sends a job to the Gearman job server, which then distributes the job to one of the available worker programs. The worker performs the task and sends the result back to the client through the job server. This allows for efficient distribution and handling of tasks, especially in a multi-server environment.

How can I install Gearman in PHP?

To install Gearman in PHP, you need to first install the Gearman job server and then the PHP extension for Gearman. The installation process may vary depending on your operating system. On a Unix-based system like Ubuntu, you can use the apt-get command to install the Gearman job server. For the PHP extension, you can use the pecl command. It’s important to ensure that you have all the necessary dependencies installed before you start the installation process.

How can I create a Gearman worker in PHP?

Creating a Gearman worker in PHP involves writing a script that registers a function with the Gearman job server and then waits for jobs to be sent from a client. The registered function will be executed whenever a client sends a job for it. The worker script should be kept running continuously to process incoming jobs.

How can I create a Gearman client in PHP?

A Gearman client in PHP is created by writing a script that sends jobs to the Gearman job server. The client can specify the function to be executed and provide any necessary data for the function. The job server will then distribute the job to an available worker.

Can Gearman handle multiple tasks simultaneously?

Yes, Gearman is designed to handle multiple tasks simultaneously. It does this by distributing jobs to multiple workers, either on the same machine or on different machines. This allows for efficient processing of tasks, especially in a high-load environment.

What are some common use cases for Gearman?

Gearman is commonly used in situations where tasks need to be distributed across multiple machines or processes. This includes scenarios like processing large amounts of data, performing time-consuming calculations, handling multiple user requests simultaneously, and running tasks in the background while the main application continues to respond to user requests.

How does Gearman handle failure of a worker?

If a Gearman worker fails while processing a job, the job server will typically reassign the job to another available worker. This ensures that jobs are not lost due to worker failures. However, the exact behavior can depend on the specific configuration of the Gearman system.

Can Gearman be used with other programming languages?

Yes, Gearman can be used with a variety of programming languages. In addition to PHP, Gearman has interfaces for languages like Python, Ruby, Java, and Perl. This makes it a versatile tool for distributed computing.

What are some alternatives to Gearman?

There are several alternatives to Gearman for distributed computing, including RabbitMQ, Redis, and Apache Kafka. These tools offer similar functionality but may have different features or performance characteristics. The choice between Gearman and these alternatives can depend on the specific requirements of your application.

Alireza Rahmani KhaliliAlireza Rahmani Khalili
View Author

To whom it may concern, I'm Alireza. For a significant chunk of my waking hours I’m a PHP expert, Author, Speaker and independent consultant on the design of enterprise web applications with Master's degrees in Computer Science. I <3 Tech, But pass time away from computers as an aspiring amateur writer, fishing, traveling, hunting, soccer- I'm a massive Liverpool FC fan!

parallel computingPHP
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week