At work we sometimes encounter things like having to publish data to multiple servers at the same time, or working on multiple tasks at the same time. The request can be processed concurrently using PHP's Curl_multi, but because of the network and data, as well as the individual servers, and so on, the response time is slow because, in the process of concurrent requests, it includes logging, processing data, and so on, waiting for the result to be processed and returned. So also can not be friendly to meet the background operation experience.
Now there is another scenario that benefits Gearman to achieve concurrent requirements. The client sends the request to Gearman's jobs, in each work for curl_multi and data processing and logging operations, and supervisor to monitor Gearman and works processes. This enables a parallel, multiple process and load-balancing 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 update, data replication
FIFO processing of restricted rate
Distributed System monitoring tasks
Gearman Working principle:
The application of Gearman usually consists 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. 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 Client and Worker APIs that allow you to communicate with Gearman Job server using these API applications. Communication between the Gearman internal Client and Worker is done through a TCP connection.
Gearman can share the workload of the workload in different machines.
Installation:
Copy 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 PHP Gearman Extensions
I use Pcel to install, you can download the source package compiled installation, but remember to install Libgearman and re2c first, otherwise extended compilation installation will be wrong.
Pecl Install Gearman #不成功并提示版本问题可以试试 pecl install gearman-1.0.3, the default seems to be 1.1.2
Compiling the installation is also very simple
Copy 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 perfect extension functions, including gearmanclient,gearmanjob,gearmantask,gearmanworker, to view the official PHP manual.
This is officially provided by example one of them, quite with an example of the processing of a concurrent distribution task
<?php $client = new Gearmanclient ();
$client->addserver ();
Initialize the results 3 "Query Results" here $userInfo = $friends = $posts = null;
This sets up what Gearman'll 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-&G
T;data ();
Break
Case ' get_latest_posts_by ': $posts = $task->data ();
Break
}
}); Here we are up multiple tasks to is execute in *as much* parallelism as Gearmand can give us $client->addtask (' Loo
Kup_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 so some very safe type checking a nd 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 performed gearman_work.php in all 3 terminals.
ryan@ryan-lamp:~$ PS aux | grep gearman* | grep-v grep
gearman 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.1
Ryan 2992 0.0 0.8 43340 9036 pts/0 s+ 14:05 0:00 php/var/www/ge armand_work.php
Ryan 3713 0.0 0.8 43340 9036 pts/1 s+ 14:05 0:00 php/var/www/gearmand_work.php
Ryan 3715 0.0 0.8 43340 9036 pts/2 s+ 14:05 0:00
To see the results of the gearman_work.php execution shell.
Copy 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 that the tasks of the past be distributed in parallel.
In the actual production environment, in order to monitor the Gearmand and work process has not been unexpectedly exited, we can use the Supervisor tool.