PS:Gearman-PHP扩展源码编译过程
#当前服务器环境 操作系统:CentOS release 6.2 PHP版本 PHP 5.4.42
gearmand源码编译,Gearman-PHP扩展依赖该库
由于扩展依赖libgearman库,并且libgearman库在gearmand源码编译中产生,所以先源码装gearmand
#gearmand依赖包安装 yum install -y gcc gcc-c++ libevent libevent-devel boost boost-devel gperf uuid libuuid libuuid-devel; #gearmand源码编译 wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz; tar -zxvf gearmand-1.1.12.tar.gz; cd gearmand-1.1.12; ./configure --prefix=/usr/local/gearmand; make && make install;
Gearman-PHP扩展安装
wget http://pecl.php.net/get/gearman-1.1.2.tgz; tar zxvf gearman-1.1.2.tgz; cd gearman-1.1.2; /usr/local/php/bin/phpize; ./configure --with-php-config=/usr/local/php/bin/php-config --with-gearman=/usr/local/gearmand; make && make install; #命令汇总: wget http://pecl.php.net/get/gearman-1.1.2.tgz;tar zxvf gearman-1.1.2.tgz;cd gearman-1.1.2;/usr/local/php/bin/phpize;./configure --with-php-config=/usr/local/php/bin/php-config --with-gearman=/usr/local/gearmand;make && make install;
将gearman.so模块信息加入php.ini
vi /usr/local/php/etc/php.ini #增加 extension_dir = './' -> extension_dir = '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/' extension=gearman.so
重启php,通过命令:php -m | grep ‘gearman’ 查询扩展是否已经安装
PHP接口函数
Gearman提供很多完善的扩展函数,包括GearmanClient,GearmanJob,GearmanTask,GearmanWorker,具体可以查看PHP官方手册.
这是官方提供的Example其中的一个,相当与一个并发的分发任务处理的例子
gearman_client.php
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', '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
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());
我在3个终端中都执行了gearman_work.php
$ 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/gearmand_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 php /var/www/gearmand_work.php
来查看下执行gearman_work.php的结果shell
Fetching... Got user info in: 3.03 seconds: string(59) "The user requested (joe@joe.com) is 7 feet tall and awesome" string(56) "The user (joe@joe.com) is 1 degree away from Kevin Bacon" string(43) "The user (joe@joe.com) has no posts, sorry!"
看到上面的3.03 seconds,说明client请求过去的任务被并行分发执行了。