参考

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扩展实现异步分步式任务的更多相关文章

  1. 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 ...

  2. mac下快速安装gearman和php扩展

    1.brew install gearman 用brew安装gearman 2.pecl install gearman 用pecl安装php的gearman扩展 3.ln -s /usr/local ...

  3. Linux 上安装Gearman及其PHP扩展

    安装Gearman服务端 # yum install -y uuid-devel libuuid libuuid-devel uuid boost-devel libevent libevent-de ...

  4. Centos7安装gearman和php扩展

    Centos7安装gearman和php扩展 标签(空格分隔): php,linux gearman所需要的依赖 yum install \ vim \ git \ gcc \ gcc-c++ \ w ...

  5. 安装Stomp扩展时错误提示error: 'zend_class_entry' has no member named 'default_properties'

    在安装stomp扩展时, 有这样的提示 error: 'zend_class_entry' has no member named 'default_properties' 交待下安装上下文, sto ...

  6. 基于Zookeeper的分步式队列系统集成案例

    基于Zookeeper的分步式队列系统集成案例 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, ...

  7. linux 下 php 安装 Gearman

    Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.   一个Gearman请求的处理过程涉及三个角色: Clien ...

  8. Mahout分步式程序开发 聚类Kmeans(转)

    Posted: Oct 14, 2013 Tags: clusterHadoopkmeansMahoutR聚类 Comments: 13 Comments Mahout分步式程序开发 聚类Kmeans ...

  9. HMX-Server C++ 分步式服务器大版本更新了(有源码)

    原文地址:http://www.cnblogs.com/hellohuang/p/6294763.html # HMX-ServerHMX-Server分步式服务器框架,主要分为网关.登录.世界.场景 ...

随机推荐

  1. Sql的一些概念

    聚合函数 聚合函数可以返回整个或者几个列或者一个列的汇总数据,它常用来计算SELECT语句查询的统计值.聚合函数经常与SELECT语句的GROUP BY 子句一同使用.

  2. java日期比较,日期计算

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 都是常用的日期之间的比较方法,供以后参考. 热身:获取当前时间 SimpleDateFormat df = new Simpl ...

  3. QueryRunner(common-dbutils.jar)

    QueryRunner update方法:* int update(String sql, Object... params) --> 可执行增.删.改语句* int update(Connec ...

  4. projecteuler Problem 9 Special Pythagorean triplet

    A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 Fo ...

  5. 微信的 JSSDK

    闲来无事,花了几个小时研究了下 微信的 JSSDK. 将学习中所用到的代码都复制到这儿,以后查看的时候方便点. PHP $AppID = ""; //APPID $AppSecre ...

  6. java8中hashMap

    摘自:http://www.importnew.com/20386.html 简介 Java为数据结构中的映射定义了一个接口java.util.Map,此接口主要有四个常用的实现类,分别是HashMa ...

  7. Java、Android 开发环境搭建

    一.准备工作 为便于管理,将java开发工具集中到一个文件夹中.创建D:\javaDevE文件夹,JDK.Android-SDK.Eclipse.tomcat等都可以安装到这个文件夹中. 二.搭建Ja ...

  8. jafka的zk数据

    查看topics: ls /brokers/topics [mytopic] 查看topic所在的broker,下面例子,mytopic在broker 0 中管理. ls /brokers/topic ...

  9. PHPRPC jsp发布服务

    PHPRPC是面向服务的高性能远程过程调用协议.PHPRPC for java 是此协议的一种实现,详细请到官方看PHPRPC的介绍 ,以其它rpc协议的性能对比请:Java.PHPRPC.Hessi ...

  10. LoadRunner ---手动关联与预关联

    手动关联                                       如果脚本很长,那么我们想找到一个脚本中哪些地方是需要关联的并不是一件容易的事情.这时,我们可以通过脚本对比的方法找 ...