zoukankan      html  css  js  c++  java
  • 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() . "
    ";
    
        exit;
    
    }
    
    
    
    echo "DONE
    ";
    
    
    
    function reverse_status($task)
    
    {
    
        echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() . 
    
             "/" . $task->taskDenominator() . "
    ";
    
    }
    
    
    
    function reverse_complete($task)
    
    {
    
        echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "
    ";
    
    }
    
    
    
    ?> 
    

    GearmanWorker.php

    <?php
    
    
    
    # The worker script
    
    
    
    echo "Starting
    ";
    
    
    
    # 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...
    ";
    
    while($gmworker->work())
    
    {
    
      if ($gmworker->returnCode() != GEARMAN_SUCCESS)
    
      {
    
        echo "return_code: " . $gmworker->returnCode() . "
    ";
    
        break;
    
      }
    
    }
    
    
    
    function reverse_fn($job)
    
    {
    
      echo "Received job: " . $job->handle() . "
    ";
    
    
    
      $workload = $job->workload();
    
      $workload_size = $job->workloadSize();
    
    
    
      echo "Workload: $workload ($workload_size)
    ";
    
    
    
      # 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
    ";
    
        $job->sendStatus($x+1, $workload_size);
    
        $job->sendData(substr($workload, $x, 1));
    
        sleep(1);
    
      }
    
    
    
      $result= strrev($workload);
    
      echo "Result: $result
    ";
    
    
    
      # 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
    
    
  • 相关阅读:
    【转】点集 凸包
    【转】Word中使用Endnote很卡解决方案
    【转】一个程序员的面试经验之谈
    【转】 std list/vector sort 排序
    std::numeric_limits<int>::max() error C2589: '(' : illegal token on right side of '::' 解决办法
    【转】Log4cpp 封装
    【转】 log4cpp 的使用
    遍历目录 删除目录中包含指定字符的文件和文件夹
    mysql主从复制架构
    mysql分区功能详细介绍,以及实例
  • 原文地址:https://www.cnblogs.com/hubery/p/5484957.html
Copyright © 2011-2022 走看看