参考

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. Oracle linux6.1配置yum本地源

    一.准备工作1. Linux安装盘插入光驱2. 挂载光驱 [root@localhost ~]# mount /dev/cdrom /mnt/ mount: block device /dev/sr0 ...

  2. [EventBus源码解析] EventBus.register 方法详述

    前情概要 在上一篇中,介绍了EventBus的基本使用方法,以及一部分进阶技巧.本篇及以后的几篇blog将会集中解析EventBus.java,看看作者是如何优雅地实现这个看似简单的事件分发/接收机制 ...

  3. 关于javascript tween的学后小感想

    今天决定了解一下tween算法,首先得下载个tween.js看看吧,好吧,有点被惊艳到了. 也让我想起了之前上数学课时,听到过的一句话:“数学世界是神秘.纯洁.有魅力的”,一直 记得这句话,期待有朝一 ...

  4. DCC Software and Graphics System

    After working with DCC software for so many years, I saw the realtime solution went forward so much, ...

  5. WebStorage 和 Cookie的区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...

  6. rediscluster 集群操作(摘抄)

    一:关于redis cluster 1:redis cluster的现状 目前redis支持的cluster特性 1):节点自动发现 2):slave->master 选举,集群容错 3):Ho ...

  7. JavaScript高级程序设计笔记之面向对象

    说起面向对象,大部分程序员首先会想到 类 .通过类可以创建许多具有共同属性以及方法的实例或者说对象.但是JavaScript并没有类的概念,而且在JavaScript中几乎一切皆对象,问题来了,Jav ...

  8. VC++ AfxBeginThread 与 CreateThread 的区别

    简言之:AfxBeginThread是MFC的全局函数,是对CreateThread的封装.    CreateThread是Win32 API函数,前者最终要调到后者.具体说来,CreateThre ...

  9. .Net的错误机制

    //优先级1    protected void Page_Error(object sender, EventArgs e)     {        Exception objErr = Serv ...

  10. A configuration with this name already exists

    进入eclipse的workspace下的目录workspace\.metadata\.plugins\org.eclipse.debug.core\.launches,如实际目录为: D:\work ...