使用yum直接安装:
yum install gearmand
安装PHP扩展:
yum install libgearman re2c
wget http://pecl.php.net/get/gearman-1.1.2.tgz
tar zxvf gearman-1.1.2.tgz
cd gearman-1.1.2
phpize
./configure
make && make install
echo "extension=gearman.so" >> /etc/php.ini
启动任务分发进程:
gearmand -d --keepalive --libtokyocabinet-file /tmp/gearmand.tch
PHP客户端,提交任务:
<?php
$client= new GearmanClient();
$client->addServer("127.0.0.1", 4730);
//发送任务到Job,处理函数和数据
//echo $client->do("reverse", "Hello World!");
echo $client->doBackground("reverse", "Hello World!");
?>
PHP执行端:
<?php
$worker= new GearmanWorker();
$worker->addServer("127.0.0.1", 4730);
//处理接收到数据的回调函数
$worker->addFunction("reverse", "reverse_function");
while ($worker->work());
function reverse_function($job)
{
for($i = 0; $i < 10; $i++){
sleep(1);
echo "{$i}\n";
}
return $job->workload();
}
?>