没有任何示例显示如何使用PHP中的Gearman.一个典型的就是这样
**CLIENT**
<?php
$client= new GearmanClient();
$client->addServer();
print $client->do("revit", "AlL THE World's a sTagE");
print "\n";
?>
**SERVER**
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("revit", "rev_it");
while ($worker->work());
function rev_it($job)
{
return strrev($job->workload());
}
?>
Run Code Online (Sandbox Code Playgroud)
我在我的64位CentOS服务器上安装了Gearmand和PECL PHP扩展,编写了这些脚本,确保Gearmand运行然后浏览到客户端.浏览器等了....
我完全预料到这一点,因为我认为Gearman服务器需要以某种方式知道它应该在收到适当的客户端请求时执行该特定的工作脚本.
我打开另一个选项卡并浏览到工作脚本,并立即在客户端脚本选项卡中找回响应.
这里似乎缺少一个链接.当一个人写一个工作脚本时,它不是必须在Gearman服务器上注册,所以后者知道用它来服务某些客户端吗?
要么我一直在谷歌搜索错误的东西,否则所有那些"如何在PHP中使用Gearman"的例子都会遗漏一些东西.有人可以帮忙吗?
你应该从控制台(或者像GearmanManager或者supervisord(或者屏幕)之类的东西)运行工作脚本,它将连接到gearmand,注册它的功能并等待工作从gearmand移交给它.
执行流程将是这样的:
在控制台终端中启动工作程序
GearmanWorker -> register function -> gearmand
Run Code Online (Sandbox Code Playgroud)
Web请求到达
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
Run Code Online (Sandbox Code Playgroud)
希望这能更好地解释事物是如何相互关联的,以及为什么如果你没有让工人实际在某个地方运行并且在客户端被执行时注册了gearmand,那么你第一次没有得到结果.