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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)