参考

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. AX 2012 query应用collections

    QueryBuildRange range; super(); this.query().dataSourceName('VendTop10VendorsByPurchase').clearDynal ...

  2. restfull api

    REST 表示状态传输.这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗.核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP 用于使它们之间调 ...

  3. php使用递归计算目录大小

    本文章向大家介绍php如何计算某个目录的大小(多少kb,多少兆m),主要使用filesize函数配合递归函数的方法来实现,需要的朋友可以参考一下本文章的源代码.php使用递归计算目录大小,主要使用fi ...

  4. 使用JDBC处理Oracle大数据

    一.Oracle中大数据处理 在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了.因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种 ...

  5. php CI ip限制

    public function index() { $ip = $this->input->ip_address(); if(!in_array($ip, $this->allowe ...

  6. MFCButton Memory leak(内存泄露问题)

    http://m.blog.csdn.net/blog/haoekin/8851219 1.无法显示右边箭头的问题 无论怎么折腾都没显示不出来,微软给的示例又能显示,度娘和谷歌也都不知道,经过不断地探 ...

  7. LESS 学习记录(简单入门)

    什么是 LESS LESS是一种动态样式语言,属于 CSS 预处理语言的一种.它使用类似 CSS 的语法,但是 赋予了动态语言的特性,比如变量.继承.运算.函数等,方便 CSS 的编写和维护. 使用 ...

  8. arpg网页游戏之地图(二)

    [转]http://www.cnblogs.com/BlueWoods/p/4684557.html 这一节说说视窗,这个视窗,也就是游戏的视角.现在的网页游戏分为2D游戏,2.5D游戏和3D游戏,2 ...

  9. 开源GIS简介.学习

    开发者都希望自己的软件能够运行在尽可能多的计算机上.然而事与愿违,摆在 GIS开发者面前的仍然是对峙的平台.J2EE随着Java5.0的发布,已经正式更名为JavaEE,而微软也正式发布了.NET2. ...

  10. VC++ CTreeCtrl 使用NM_CLICK和TVN_SELCHANGED

    //这是当CTREECTRL控件点击时NM_CLICK的处理函数 void CDriverSelCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) { C ...