Introduction to PHP concurrent multi-process processing tool Gearman and gearman_PHP tutorial

Source: Internet
Author: User
Introduction to the PHP concurrent multi-process processing tool Gearman, and the tool gearman. Introduction to the use of PHP concurrent multi-process processing tool Gearman, we sometimes encounter the need to simultaneously publish data to multiple servers in gearman's work, or you can use the Gearman tool to process multiple concurrent PHP processes at the same time.

At work, we sometimes encounter the need to simultaneously publish data to multiple servers, or simultaneously process multiple tasks. The PHP curl_multi method can be used to concurrently process requests. However, the response time of concurrent processing is slow due to network and data, various servers, and so on, because the process of concurrent requests also includes logging, processing data, and Other Logic, waiting for processing results and returning, it cannot meet the background operation experience.

There is another solution that leverages Gearman to implement concurrency requirements. The Client sends requests to Jobs of Gearman, and then performs curl_multi, data processing, logs, and other operations in each Work. at the same time, supervisor is used to monitor the processes of Gearman and Works, in this way, a parallel multi-process and load balancing solution can be implemented.

What can Gearman do:

Asynchronous Processing: image processing, order processing, batch mail/notification, etc.
High CPU or Memory processing: large-capacity data processing, MapReduce operations, log aggregation, and video encoding
Distributed and parallel processing
Scheduled processing: Incremental update and data replication
Maximum rate of FIFO processing
Distributed System monitoring tasks

How Gearman works:
Applications using Gearman generally consist of three parts: a Client, a Worker, and a task server. The role of the Client is to submit a Job task to the Job Server. The Job Server will find a suitable Worker to complete this task. The Worker executes the Job sent by the Client and returns the result to the Client through the Job Server. Gearman provides Client and Worker APIs, which can be used to communicate with Gearman Job Server. The communication between the Client and Worker in Gearman is performed through a TCP connection.

Gearman can share the workload to different machines.


The code is as follows:
Yum install-y gearmand


Install PHP Gearman extension
I use pcel for installation. you can also download the source package to compile and install it. but remember to install libgearman and re2c first. Otherwise, the extended compilation and installation will fail.

Pecl install gearman # not successful and prompt version problems can try pecl install gearman-1.0.3, the default seems to be 1.1.2
Compilation and installation are also simple
The code is as follows:
Http:// wget-c
Tar zxvf gearman-1.1.1.tgz
Make & make install
Echo "extension = gearman. so">/etc/php. ini

PHP interface functions
Gearman provides many complete extension functions, including GearmanClient, GearmanJob, GearmanTask, and GearmanWorker. for details, refer to the official PHP Manual.
This is one of the official Example, equivalent to an Example of concurrent distribution task processing.

<?php$client = new GearmanClient();$client->addServer();// initialize the results of our 3 "query results" here$userInfo = $friends = $posts = null;// This sets up what gearman will callback to as tasks are returned to us.// The $context helps us know which function is being returned so we can// handle it correctly.$client->setCompleteCallback(function(GearmanTask $task, $context) use (&$userInfo, &$friends, &$posts) {switch ($context){case 'lookup_user':$userInfo = $task->data();break;case 'baconate':$friends = $task->data();break;case 'get_latest_posts_by':$posts = $task->data();break;}});// Here we queue up multiple tasks to be execute in *as much* parallelism as gearmand can give us$client->addTask('lookup_user', '', 'lookup_user');$client->addTask('baconate', '', 'baconate');$client->addTask('get_latest_posts_by', '', 'get_latest_posts_by');echo "Fetching...\n";$start = microtime(true);$client->runTasks();$totaltime = number_format(microtime(true) - $start, 2);echo "Got user info in: $totaltime seconds:\n";var_dump($userInfo, $friends, $posts);


<?php$worker = new GearmanWorker();$worker->addServer();$worker->addFunction('lookup_user', function(GearmanJob $job) {// normally you'd so some very safe type checking and query binding to a database here.// ...and we're gonna fake that.sleep(3);return 'The user requested (' . $job->workload() . ') is 7 feet tall and awesome';});$worker->addFunction('baconate', function(GearmanJob $job) {sleep(3);return 'The user (' . $job->workload() . ') is 1 degree away from Kevin Bacon';});$worker->addFunction('get_latest_posts_by', function(GearmanJob $job) {sleep(3);return 'The user (' . $job->workload() . ') has no posts, sorry!';});while ($worker->work());

I have executed gearman_work.php in all three terminals.

ryan@ryan-lamp:~$ ps aux | grep gearman* | grep -v grepgearman 1504 0.0 0.1 60536 1264 ? Ssl 11:06 0:00 /usr/sbin/gearmand --pid-file=/var/run/gearman/ --user=gearman --daemon --log-file=/var/log/gearman-job-server/gearman.log --listen= 2992 0.0 0.8 43340 9036 pts/0 S+ 14:05 0:00 php /var/www/gearmand_work.phpryan 3713 0.0 0.8 43340 9036 pts/1 S+ 14:05 0:00 php /var/www/gearmand_work.phpryan 3715 0.0 0.8 43340 9036 pts/2 S+ 14:05 0:00 php /var/www/gearmand_work.php

To view the result shell of running gearman_work.php.

The code is as follows:
Got user info in: 3.03 seconds:
String (59) "The user requested ( is 7 feet tall and awesome"
String (56) "The user ( is 1 degree away from Kevin Bacon"
String (43) "The user ( has no posts, sorry! "

Seeing the above 3.03 seconds, it means that the client request's previous tasks have been distributed and executed in parallel.
In the actual production environment, we can use the Supervisor tool to monitor the gearmand and work processes without being unexpectedly exited.

In the pipeline work, we sometimes encounter the need to publish data to multiple servers at the same time, or to process multiple tasks at the same time...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.