php安装gearman扩展实现异步分步式任务
参考:
1.小喵爱你的博客
2.PHP Manual
依赖
1.gcc44
2.boost >=1.39
3.libevent
4.php5.3+
5.update ld.so.conf
安装依赖(Ubuntu 14.04 LTS)
$ sudo apt-get install libboost-dev
$ sudo apt-get install libevent-dev
$ sudo apt-get install libgearman-dev
$ sudo apt-get install gearman-job-server
安装gearman到PHP
$ wget http://pecl.php.net/get/gearman-1.1.2.tgz
$ tar xvzf 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
$ make
$ sudo make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
编辑php.ini,添加:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/gearman.so
重启apache,检查验证
$ sduo service httpd restart
$ /usr/local/php/bin/php --info | grep gearman
gearman
gearman support => enabled
libgearman version => 1.0.6
PWD => /home/www/Downloads/gearman-1.1.2
_SERVER["PWD"] => /home/www/Downloads/gearman-1.1.2
启动gearman服务
$ sudo gearmand -d -u daemon --log-file=/tmp/gearman.log
运行DEMO程序
GearmanClient.php
<?php
# The client script
# create our gearman client
$gmc= new GearmanClient();
# add the default job server
$gmc->addServer();
# set a couple of callbacks so we can track progress
$gmc->setCompleteCallback("reverse_complete");
$gmc->setStatusCallback("reverse_status");
# add a task for the "reverse" function
$task= $gmc->addTask("reverse", "Hello World!", null, "1");
# add another task, but this one to run in the background
$task= $gmc->addTaskBackground("reverse", "!dlroW olleH", null, "2");
if (! $gmc->runTasks())
{
echo "ERROR " . $gmc->error() . "\n";
exit;
}
echo "DONE\n";
function reverse_status($task)
{
echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() .
"/" . $task->taskDenominator() . "\n";
}
function reverse_complete($task)
{
echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}
?>
GearmanWorker.php
<?php
# The worker script
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
$gmworker->addServer();
# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job)
{
echo "Received job: " . $job->handle() . "\n";
$workload = $job->workload();
$workload_size = $job->workloadSize();
echo "Workload: $workload ($workload_size)\n";
# This status loop is not needed, just showing how it works
for ($x= 0; $x < $workload_size; $x++)
{
echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";
$job->sendStatus($x+1, $workload_size);
$job->sendData(substr($workload, $x, 1));
sleep(1);
}
$result= strrev($workload);
echo "Result: $result\n";
# Return what we want to send back to the client.
return $result;
}
?>
命令行执行后分别输出:
$ /usr/local/php/bin/php GearmanClient.php
STATUS: 1, H:hubery-VirtualBox:2 - 1/12
STATUS: 1, H:hubery-VirtualBox:2 - 2/12
STATUS: 1, H:hubery-VirtualBox:2 - 3/12
STATUS: 1, H:hubery-VirtualBox:2 - 4/12
STATUS: 1, H:hubery-VirtualBox:2 - 5/12
STATUS: 1, H:hubery-VirtualBox:2 - 6/12
STATUS: 1, H:hubery-VirtualBox:2 - 7/12
STATUS: 1, H:hubery-VirtualBox:2 - 8/12
STATUS: 1, H:hubery-VirtualBox:2 - 9/12
STATUS: 1, H:hubery-VirtualBox:2 - 10/12
STATUS: 1, H:hubery-VirtualBox:2 - 11/12
STATUS: 1, H:hubery-VirtualBox:2 - 12/12
COMPLETE: 1, !dlroW olleH
DONE
$ /usr/local/php/bin/php GearmanWorker.php
Starting
Waiting for job...
Received job: H:hubery-VirtualBox:1
Workload: !dlroW olleH (12)
Sending status: 1/12 complete
Sending status: 2/12 complete
Sending status: 3/12 complete
Sending status: 4/12 complete
Sending status: 5/12 complete
Sending status: 6/12 complete
Sending status: 7/12 complete
Sending status: 8/12 complete
Sending status: 9/12 complete
Sending status: 10/12 complete
Sending status: 11/12 complete
Sending status: 12/12 complete
Result: Hello World!
Received job: H:hubery-VirtualBox:2
Workload: Hello World! (12)
Sending status: 1/12 complete
Sending status: 2/12 complete
Sending status: 3/12 complete
Sending status: 4/12 complete
Sending status: 5/12 complete
Sending status: 6/12 complete
Sending status: 7/12 complete
Sending status: 8/12 complete
Sending status: 9/12 complete
Sending status: 10/12 complete
Sending status: 11/12 complete
Sending status: 12/12 complete
Result: !dlroW olleH
php安装gearman扩展实现异步分步式任务的更多相关文章
- mac机上搭建php56/nginx 1.8.x/thinkphp 3.2.x/gearman扩展/seaslog扩展/redis扩展环境
php的各种扩展配置起来实在不容易,记录一下备忘: 一.php56 安装 虽然php7出来了,但是没用过,不知道有没有坑,这里仍然使用php5.6版本 1.1 安装php/php-pfm brew u ...
- mac下快速安装gearman和php扩展
1.brew install gearman 用brew安装gearman 2.pecl install gearman 用pecl安装php的gearman扩展 3.ln -s /usr/local ...
- Linux 上安装Gearman及其PHP扩展
安装Gearman服务端 # yum install -y uuid-devel libuuid libuuid-devel uuid boost-devel libevent libevent-de ...
- Centos7安装gearman和php扩展
Centos7安装gearman和php扩展 标签(空格分隔): php,linux gearman所需要的依赖 yum install \ vim \ git \ gcc \ gcc-c++ \ w ...
- 安装Stomp扩展时错误提示error: 'zend_class_entry' has no member named 'default_properties'
在安装stomp扩展时, 有这样的提示 error: 'zend_class_entry' has no member named 'default_properties' 交待下安装上下文, sto ...
- 基于Zookeeper的分步式队列系统集成案例
基于Zookeeper的分步式队列系统集成案例 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, ...
- linux 下 php 安装 Gearman
Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务. 一个Gearman请求的处理过程涉及三个角色: Clien ...
- Mahout分步式程序开发 聚类Kmeans(转)
Posted: Oct 14, 2013 Tags: clusterHadoopkmeansMahoutR聚类 Comments: 13 Comments Mahout分步式程序开发 聚类Kmeans ...
- HMX-Server C++ 分步式服务器大版本更新了(有源码)
原文地址:http://www.cnblogs.com/hellohuang/p/6294763.html # HMX-ServerHMX-Server分步式服务器框架,主要分为网关.登录.世界.场景 ...
随机推荐
- 纸上谈兵:队列(queue)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 队列(queue)是一个简单而常见的数据结构.队列也是有序的元素集合.队列最大的特 ...
- Linux:基础配置--备忘
1.网络 /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=08:00:27:77:F3:A1 TYPE=Ethernet UU ...
- Java IO流体系中常用的流分类
Java输入/输出流体系中常用的流分类(表内容来自java疯狂讲义) 注:下表中带下划线的是抽象类,不能创建对象.粗体部分是节点流,其他就是常用的处理流. 流分类 使用分类 字节输入流 字节输出流 字 ...
- 多媒体(4):JPEG图像压缩编码
(重要的事放前面)此JPEG的C++实现见 https://github.com/chencjGene/SoftEngineering/tree/master/JPEG 目录 多媒体(1):MCI接口 ...
- Log4Net日志的配置
<configuration> <configSections> <section name="log4net" type="log ...
- brackets快捷键使用
ctrl+b 当选中一个文本时,会出现相同的文本,被高亮显示 按ctrl+b 相同的文本就全部获得了焦点 这样就可以同时更改这些相同的文本ctrl+e 打开或关闭快速编辑alt+u 注释ctrl ...
- web测试特别点
1.浏览器的后退按钮 提交表单一条已经成功提交的记录,back后再提交,看系统会如何处理. 检查多次使用back健的情况在有back的地方,back,回到原来的页面,再back,重复几次,看是否会 ...
- c++学习笔记——构造函数
构造函数定义:每个类都分别定义了它的对象被初始化的方式,类通过一个或几个特殊的成员函数来控制其对象的初始化过程,这些函数叫做构造函数. 需要注意的几点: 1:构造函数不能被声明为const的,当我们创 ...
- EAS linux挂载数据盘
查看数据盘名称 fdisk -l 假设没有挂载的数据盘为/dev/xvdb 格式化数据盘 mkfs.ext3 /dev/xvdb 添加自动挂载 mkdir /data echo '/dev/xvdb ...
- Factstone Benchmark
[问题描述] Amtel已经宣布,到2010年,它将发行128位计算机芯片:到2020年,它将发行256位计算机:等等,Amtel坚持每持续十年将其字大小翻一番的战略.(Amtel于2000年发行了6 ...