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

posted @ 2016-05-12 11:26  回首郑板桥  阅读(425)  评论(0编辑  收藏  举报