参考

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

    一.字符串的基本使用 #!/usr/bin/env python #!-*- coding:utf-8 -*- #!/usr/bin/env python  指定解释器为python abc='hel ...

  2. JNI日志调试LOG和中文乱码

    添加日志: 1. 增加log支持. Android.mk文件增加LOCAL_LDLIBS += -llog 2. C代码中增加(放在最前面) #include <android/log.h> ...

  3. 关于rand()与srand()函数

    rand函数功能为获取一个伪随机数(伪随机数的概念下面会有介绍). 一.函数名: rand(); 二.声明: int rand(); 三.所在头文件: stdlib.h 四.功能: 返回一个伪随机数. ...

  4. Python自动化 【第十篇】:Python进阶-多进程/协程/事件驱动与Select\Poll\Epoll异步IO

    本节内容: 多进程 协程 事件驱动与Select\Poll\Epoll异步IO   1.  多进程 启动多个进程 进程中启进程 父进程与子进程 进程间通信 不同进程间内存是不共享的,要想实现两个进程间 ...

  5. iOS 调用拍照、选择本地相册、上传功能---未完善。

    1.新建viewController 拖入一个Button,添加点击事件,使用代理方法 <UIActionSheetDelegate,UIImagePickerControllerDelegat ...

  6. Windows程序设计(第五版)学习:第二章 Unicode简介

    第二章 Unicode简介 1,Windows通过双字节技术DBCS解决这个问题,代码页定义不同的字符集,称为ANSI字符集,比如日文为CP932,韩文为CP949,繁体中文为CP950,简体中文为C ...

  7. C语言基础_2

    scanf函数可以从键盘上读取数据并记录到变量中.为了使用这个函数也需要在文件开头使用如下的预处理指令#include <stdio.h>scanf函数使用的时候所需要的初始数据和prin ...

  8. Unix网络编程 -- ubuntu下搭建编译环境( 解决unp.h 编译等问题)

    1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下载unpv13e  http://ishare.i ...

  9. wget net-tools

    新安装的centos7 minimal 没有安装 wget 需要安装一下,才能安装lnmp yum -y install wget yum -y install net-tools

  10. P1159岳麓山上打水

    P1159岳麓山上打水 https://vijos.org/p/1159 dfsID,第一次听说这东西,但是感觉不太靠谱啊. 一开始的时候,想到了排个序后,然后进行dp,如果要输出字典序最小其实还是可 ...