Getting Started with Gearman in PHP: Install, Configure, and Run Distributed Tasks

This guide explains how to install the Gearman job server and its PHP extension on Linux, start the daemon, and build a simple PHP worker and client that perform a string‑reversal task, illustrating the basics of distributed task processing with Gearman.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Getting Started with Gearman in PHP: Install, Configure, and Run Distributed Tasks

Overview

Gearman is an open‑source distributed task queue that splits complex jobs into independent sub‑tasks, dispatches them to multiple servers, and aggregates the results. It is useful for asynchronous processing, batch computation, or any scenario requiring distributed collaboration.

Installation

Install the Gearman daemon on a Linux host:

// RHEL/Fedora
yum install gearmand

// Debian/Ubuntu
apt install gearman-job-server

Starting the Service

After installation, launch the daemon in the background: gearmand -d The -d flag runs Gearman as a daemon (background process).

PHP Extension Installation

Install the Gearman PHP extension via PECL and enable it in php.ini:

pecl install gearman
extension=gearman.so

Getting Started with PHP

Simple string‑reversal task

Worker (Server‑side) Code

<?php
// worker.php
$worker = new GearmanWorker();
$worker->addServer(); // connect to default port 4730
$worker->addFunction("reverse_string", function($job) {
    $workload = $job->workload(); // get task data
    $result = strrev($workload); // reverse the string
    return $result; // return result
});

echo "Worker 已启动,等待任务...
";
while ($worker->work());

Client (Producer) Code

<?php
// client.php
$client = new GearmanClient();
$client->addServer(); // connect to server

$text = "Hello Gearman!"; // task payload
$result = $client->do("reverse_string", $text); // synchronous call

echo "原始字符串: {$text}
";
echo "反转结果: {$result}
";

Run Flow

Start the worker: php worker.php Execute the client:

php client.php
The client sends the task to the worker, the worker processes it, and the result is returned to the client.

This example demonstrates the basic steps to use Gearman with PHP. In real projects you can define additional task types, implement more complex processing logic, and fully exploit Gearman's distributed scheduling capabilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPInstallationDistributed Tasksjob queueGearman
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.