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?

图片转代码服务由CSDN问答提供 功能建议

没有关于如何在PHP中使用Gearman的示例。 典型的一个就是这样的

  ** CLIENT ** 
 
&lt;?php 
 $ client = new GearmanClient(); 
 $ client-&gt;  addServer(); 
 print $ client-&gt; do(“revit”,“AlL the World's sTagE”); 
打印“
”; 
?&gt; 
 
 **服务器** \  n 
&lt;?php 
 $ worker = new GearmanWorker(); 
 $ worker-&gt; addServer(); 
 $ worker-&gt; addFunction(“revit”,“rev_it”); 
 while($  worker-&gt; work()); 
 
函数rev_it($ job)
 {
 return strrev($ job-&gt; workload()); 
} 
?&gt; 
   
 
 

我在我的64位CentOS服务器上安装了Gearmand和PECL PHP扩展,编写了这些脚本,确保Gearmand正在运行,然后浏览到客户端。 浏览器等了....

我完全预料到这一点,因为我认为Gearman服务器需要以某种方式知道它应该在收到适当的客户端请求时执行该特定的工作脚本。 \ n

我打开了另一个标签并浏览了工作人员脚本并立即在客户端脚本选项卡中找回了响应。

这里似乎缺少一个链接。 当一个人编写一个工作脚本时,它不是必须在Gearman服务器上注册,所以后者知道用它来服务某些客户端吗?

要么我一直在谷歌搜索错误的东西 或者所有那些“如何使用PHP中的Gearman”示例都遗漏了一些东西。 有人可以帮忙吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论

相关推荐 更多相似问题