Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Using Gearman with PHP: Setup, Worker and Client Scripts, and Deployment

Using Gearman with PHP: Setup, Worker and Client Scripts, and Deployment

This article provides a step‑by‑step guide for configuring Gearman persistence, writing PHP worker and client scripts, setting up GearmanManager, launching the gearmand server with MySQL storage, and running the client to dispatch thousands of email jobs, illustrating fault‑tolerant asynchronous processing.

Preparation : To avoid downtime during business processing, configure Gearman's persistence method and use GearmanManager to manage worker scripts for easier testing.

Write the worker script (sendEmail.php) :

<?php
// Note: function name matches file name
function sendEmail($job) {
    $workId = uniqid();
    // workload() gets the serialized data from the client
    $data = json_decode($job->workload(), true);
    // Simulate processing (e.g., sending an email)
    sleep(1);
    echo "workId: {$workId} 发送 {$data['email']} 成功
";
}

Write the client script (client.php) :

<?php
// Create a client
$client = new GearmanClient();
// Add a job server
$client->addServer('127.0.0.1', 4730);
$cnt = 5000;
$ret = array();
// Loop to send 5000 email jobs
for ($i = 0; $i < $cnt; ++$i) {
    // doBackground is asynchronous and returns a handle
    $ret[$i] = $client->doBackground('sendEmail', json_encode(array(
        'email' => "{$i}@qq.com",
        'title' => "邮件标题{$i}",
        'body'  => "我是内容{$i}"
    )));
}

Configure GearmanManager : Edit /data/GearmanManager/etc/GearmanManager.ini and add the following section to start five dedicated processes for the sendEmail job.

[sendEmail]
; specify 5 processes
dedicated_count=5
; only run sendEmail jobs
dedicated_only=1

Start the gearmand server with MySQL persistence:

gearmand -d -q mysql \
    --mysql-host=192.168.1.100 \
    --mysql-port=3306 \
    --mysql-user=gearman \
    --mysql-password=123456 \
    --mysql-db=gearman \
    --mysql-table=gearman_queue &

Launch GearmanManager :

cd /data/GearmanManager
./bin/pecl_manager.php -c /data/GearmanManager/etc/GearmanManager.ini -vvv

Run the client to dispatch the jobs:

/data/php56/bin/php /data/client.php

When the manager is stopped with Ctrl+C, the workers are killed but the client continues to send requests; the job data remains stored in MySQL. Restarting the workers causes Gearman to reload and process the pending jobs.

Note: If Gearman cannot connect to MySQL on a Windows host, temporarily disable the Windows 10 firewall and enable ICMP echo replies.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
backendPHPjob queueGearman

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion