First, Introduction
Gearman is a tool that can delegate work to other machines or processes.
Website address: http://gearman.org/
Gearman usually consists of three parts, client+worker+ task server, the worker executes the job sent by the client, and then returns to the client through the job server. Gearman provides the client and worker APIs to communicate with the job server using these APIs.
Here's how it works:
Second, installation
Installation official instruction at: http://gearman.org/getting-started/
The following is an introduction to the specific installation process in Ubuntu:
1. The ubuntu14.04,python-software-properties was replaced by the Software-properties-common.
sudo apt-get install software-properties-commonsudo add-apt-repository ppa:gearman-developers/ppasudo apt-get update
2. Install Gearmanjobserver, Dev Tools, Perform Upgrade
sudo apt-get install gearman-job-server libgearman-devsudo apt-get install gearman-toolssudo apt-get upgrade
3. If no pecl, install pecl and use Pecl to install Gearman (cli,client,worker)
sudo apt-get install php-pearsudo apt-get install php5-devsudo pecl install gearman
4. Modify the PHP.ini (CLI and Server) to the use Gearman
sudo vi /etc/php5/cli/php.ini
Add in the dynamic extensions:
Extension=gearman.so
Third, use Gearman in PHP
1, PHP call Gearman method
PHP View version
运行方式:php test.php
Build a worker
addServer(); $worker->addFunction("reverse", "my_reverse_function"); while ($worker->work()); function my_reverse_function($job){ return strrev($job->workload()); }?>
After this run, you need to CTRL + C to terminate the program. This code establishes a Worker object, adds the default server, and registers the reverse callback function. The program goes into a dead loop, and whenever a job enters, the callback function executes, which simply reverses the string and returns.
Build a client
Client-like worker, build the following server.php
addServer();print $client->do("reverse","Hello World!");?>
Look at the results of the execution:
Build a picture resize worker
addServer();$worker->addFunction("resize", "my_resize_function");while ($worker->work());function my_resize_function($job){ $thumb = new Imagick(); $thumb->readImageBlob($job->workload()); if ($thumb->getImageHeight() > 600) $thumb->scaleImage(0, 600); else if ($thumb->getImageWidth() > 800) $thumb->scaleImage(800, 0); return $thumb->getImageBlob();}?>
PHP requires ImageMagick support, installation method:
sudo wget http://www.imagemagick.org/download/ImageMagick.tar.gztar -xzvf ImageMagick.tar.gzcd ImageMagicksudo ./configure -prefix=/usr/local/imagemagick -enable-lzw -with-modules && make && make install
Resize Client
This is the way to invoke the client using the command
gearman -f resize < full.jpg > thumb.jpg
2. Use the Gearman example directly from the command line without using PHP
Start a terminal run daemon
sudo gearmand --daemon
Start a worker, call the LS command here
gearman -w -f ls -- ls -lh
Start a client
gearman -f ls < /dev/null
You can see the output results.
Reference URL: http://gearman.org/getting-started/#client
The above describes the introduction of Gearman notes, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.