dongluan6784 2013-12-28 07:01
浏览 78
已采纳

PHP Gearman示例 - 缺失的链接?

There is no dearth of examples showing how to use Gearman from PHP out there. A typical one would go like this

**CLIENT**

<?php
  $client= new GearmanClient();
  $client->addServer();
  print $client->do("revit", "AlL THE World's a sTagE");
  print "
";
?>

**SERVER**

<?php
   $worker= new GearmanWorker();
   $worker->addServer();
   $worker->addFunction("revit", "rev_it");
   while ($worker->work());

   function rev_it($job)
   {
      return strrev($job->workload());
   }
?>

I installed Gearmand and the PECL PHP extension on my 64 bit CentOS server, wrote up these scripts, ensured that Gearmand was running and then browsed to the client. The browser waited... .

I had fully expected this since I figured that the Gearman server needs to somehow know that it should execute that particular worker script when it receives an appropriate client request.

I opened another tab and browsed to the worker script and promptly got back a response in the client script tab.

There appears to be a missing link here. When one writes a worker script does it not somehow have to be registered with the Gearman server so the latter knows to use it to service certain clients?

Either I have been Googling for the wrong things or else all those "How to use Gearman in PHP" examples leave out something. Could someone here help?

  • 写回答

1条回答 默认 最新

  • dtqi87613 2013-12-29 17:42
    关注

    You're supposed to run the worker script from the console (or under something like GearmanManager or supervisord (or screen)), where it'll connect to gearmand, register it function and wait for work to be handed over to it from gearmand.

    The flow of execution would be something like this:

    Start a worker in a console terminal

    GearmanWorker -> register function              -> gearmand
    

    A web request arrives

    browser       -> request page                   -> GearmanClient
    GearmanClient -> perform this function          -> gearmand
    gearmand      -> here, do this function         -> GearmanWorker
    GearmanWorker -> here's the result              -> gearmand
    gearmand      -> we're done, this is the result -> GearmanClient
    GearmanClient -> return data to executing php   -> browser
    

    Hopefully that explains a bit more how things are interconnected, and why you didn't get a result the first time if you didn't have the worker actually running somewhere and registered with gearmand when the client were executed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗