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.

Installation:

Copy the Code code as follows:
RPM-IVH http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noarch.rpm
Yum Install-y Gearmand

Start:
Gearmand-d

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:
Wget-c http://pecl.php.net/get/gearman-1.1.1.tgz
Tar zxvf gearman-1.1.1.tgz
Phpize
./configure
Make && make install
echo "extension=gearman.so" >>/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 ', ' joe@joe.com ', ' Lookup_user '), $client->addtask (' baconate ', ' joe@joe.com ', ' baconate '); $client AddTask (' get_latest_posts_by ', ' joe@joe.com ', ' 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);

gearman_work.php

<?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/gearmand.pid--user=gearman--daemon--log-file=/var/ Log/gearman-job-server/gearman.log--listen=127.0.0.1ryan 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 (joe@joe.com) is 7 feet tall and awesome"
String "The user (joe@joe.com) is 1 degree away from Kevin Bacon"
String "The user (joe@joe.com) 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.

http://www.bkjia.com/PHPjc/1127877.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127877.html 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 ...

  • 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: info-contact@alibabacloud.com 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.