PHP concurrent Multi-process processing tool Gearman use Introduction, tool gearman_php Tutorial

Source: Internet
Author: User

PHP concurrent Multi-process processing tool Gearman use introduction, sharp weapon Gearman

We sometimes encounter tasks such as the need to publish data to multiple servers at the same time, or to work on more than one task at a time. Requests can be processed concurrently using PHP's Curl_multi, but because of the network and data, as well as the various servers and so on, the response time of this concurrency process is slow because the process of concurrent requests includes logging, processing data and other logic, waiting for the result to be processed and returning. So it is not friendly to meet the background operation experience.

Now there is another option, Gearman, to implement concurrency requirements. Through the client sends the request to Gearman's jobs, in each work to carry on the Curl_multi and the data processing and the log and so on some operation, simultaneously uses the supervisor to monitor Gearman as well as works's process, This enables a parallel, multi-process and load-balanced scenario.

What Gearman can do:

Asynchronous processing: Image processing, order Processing, bulk mail/notification, etc.
Requires high CPU or memory processing: large-capacity data processing, mapreduce operations, log aggregation, Video coding
Distributed and parallel processing
Timed Processing: Incremental updates, data replication
FIFO Processing of throttling rates
Distributed System monitoring tasks

Gearman Working principle:
Applications that use Gearman typically consist of three parts: a client, a worker, and a task server. The role of the client is to present a job task to the job server task servers. The job Server will look for a suitable Worker to complete the task. The Worker executes the job sent by the client and returns the result to the client through the job Server. Gearman provides the Client and Worker APIs that enable the use of these API applications to communicate with the Gearman Job server. Communication between the Gearman internal Client and Worker is done through a TCP connection.

The Gearman can share the load of work to different machines.


Copy the Code code as follows:
Yum Install-y Gearmand


Installing the PHP gearman extension
I use Pcel to install, you can also download the source package compiled installation, but remember to install Libgearman and RE2C, otherwise the extension compiled installation error.

Pecl Install Gearman #不成功并提示版本问题可以试试 pecl install gearman-1.0.3, default appears to be 1.1.2
Compiling and installing is also simple
Copy the Code code as follows:
Tar zxvf gearman-1.1.1.tgz
Make && make install
echo "" >>/etc/php.ini

PHP interface functions
Gearman offers a number of well-established extension functions, including gearmanclient,gearmanjob,gearmantask,gearmanworker, which can be viewed in the official PHP manual.
This is an example of the official offer of example one, quite with a concurrent distribution task processing

<?php$client = new Gearmanclient (); $client->addserver ();//Initialize the results of our 3 "Query results" Here$user Info = $friends = $posts = null;//This sets up what Gearman would callback to as tasks is returned to us.//the $context Helps us know which function is being returned so we can//handle it correctly. $client->setcompletecallback (function (Ge Armantask $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 being execute in *as much* parallelism as Gearmand can give Us$client->addtask (' Looku P_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, some very safe type checking and query binding to a database here.//... and we ' re g Onna 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 executed the gearman_work.php in all 3 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/ Log/gearman-job-server/gearman.log--listen= 2992 0.0 0.8 43340 9036 pts/0 s+ 14:05 0:00 Php/var/www/gearman d_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 see the results of executing gearman_work.php shell

Copy the Code code as follows:
Fetching ...
Got User info in:3.03 seconds:
String "The user requested ( is 7 feet tall and awesome"
String "The user ( is 1 degree away from Kevin Bacon"
String "The user ( has no posts, sorry!"

See the 3.03 seconds above, stating that the client requested the past tasks to be executed in parallel.
In the actual production environment, to monitor the Gearmand and work process has not been unexpectedly exited, we can use Supervisor this tool. true techarticle PHP Concurrent Multi-process processing tool Gearman use of the introduction, the tool Gearman work we sometimes encounter such as the need to publish data to multiple servers at the same time, or at the same time processing multiple any ...

  • 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.