我试图让 gearman 在 php 使用 $gmclient->do("somefunction", "somedata") 向 php 发出请求后从函数返回一个值。然而,php 客户端只是超时。我使用的确切代码直接来自 php 手册。我正在使用 http://docs.php.net/manual/ 中的示例#1 en/gearmanclient.do.php
浏览器给我这个消息:
该网页不可用。
网页位于
http://yoursite.com/client.php
可能会暂时关闭或者可能
已永久移至新网站
地址。
有关此错误的更多信息。以下
是原始错误消息
错误 324(net::ERR_EMPTY_RESPONSE):
未知错误。
如果这有助于详细说明错误消息,则浏览器是 Chrome。
如果有影响,worker.php 文件将使用命令“php worker.php”在终端窗口中执行。我正在 Ubuntu 9.10 Karmic Koala 上运行。我使用 http://blog.stuartherbert.com/php/2010/02/26/getting-gearman-up-and-running-on-ubuntu-karmic/
我检查了终端窗口,gearman是获取请求并将结果回显到终端 - 它只是不发送回客户端。
最终目标是让 gearman 将已执行函数的返回值返回给客户端,并将该值显示给用户。
更新:
根据要求,代码如下:
worker.php(工人)
<?php
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
$gmworker->addServer();
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job)
{
return strrev($job->workload());
}
?>
client.php(客户端代码 - 这是我正在加载浏览器的页面)
<?php
# Client code
echo "Starting\n";
# Create our client object.
$gmclient= new GearmanClient();
# Add default server (localhost).
$gmclient->addServer();
echo "Sending job\n";
$result = $gmclient->do("reverse", "Hello!");
echo "Success: $result\n";
?>
下面的评论说它正在工作。我重复一遍,它是不工作。它似乎有效,因为我将 $gmclient->do 更改为 $gmclient->doBackground ,它输出作业 ID,而不是函数的实际结果。
最终更新(包含解决方案)
经过一番工作,我发现这不是编码错误。 Gearman 安装不正确。我决定手动执行操作,而不是使用 apt-get install。我从 gearman 网站 (http://gearman.org/index.php?id=download) 下载了 gearmand (c)。然后我也使用了 gearman 网站上的教程,从 http://gearman.org/index 开始。 php?id=getting_started 然后 http://gearman.org/index.php? id=gearman_php_extension
I am trying to get gearman to return a value to php from a function after php makes a request to it using $gmclient->do("somefunction", "somedata"). However, the php client simply times out. The exact code I am using is straight from the php manual. I am using example #1 from http://docs.php.net/manual/en/gearmanclient.do.php
The browser gives me this message:
This webpage is not available.
The webpage at
http://yoursite.com/client.php
might be temporarily down or it may
have moved permanently to a new web
address.
More information on this error. Below
is the original error message
Error 324 (net::ERR_EMPTY_RESPONSE):
Unknown error.
The browser is Chrome if that helps to elaborate the error message.
In case it makes a difference, the worker.php file is being executed in a terminal window using the command "php worker.php". I am running on Ubuntu 9.10 Karmic Koala. I installed gearman using the directions found at http://blog.stuartherbert.com/php/2010/02/26/getting-gearman-up-and-running-on-ubuntu-karmic/
I checked the terminal window and gearman is getting the request and echos the results into the terminal - it just not sent back to the client.
The end goal is to get gearman to return to the client the return value from the function that was executed and display that value to the user.
UPDATE:
As requested, the code is below:
worker.php (the worker)
<?php
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
$gmworker->addServer();
# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");
print "Waiting for job...\n";
while($gmworker->work())
{
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job)
{
return strrev($job->workload());
}
?>
client.php (client code - this is the page I am loading the browser)
<?php
# Client code
echo "Starting\n";
# Create our client object.
$gmclient= new GearmanClient();
# Add default server (localhost).
$gmclient->addServer();
echo "Sending job\n";
$result = $gmclient->do("reverse", "Hello!");
echo "Success: $result\n";
?>
The comments below where it said it was working.. I repeat, it was NOT working. It only appeared to work because I changed $gmclient->do to $gmclient->doBackground which output the job ID, not the actual result from the function.
FINAL UPDATE (WITH SOLUTION)
After some work, I've figured out that it was not a coding error. Gearman was improperly installed. Instead of using apt-get install, I decided to do things manually. I downloaded the gearmand (c) from the gearman site (http://gearman.org/index.php?id=download). I then used the tutorials on the gearman site as well starting with http://gearman.org/index.php?id=getting_started and then http://gearman.org/index.php?id=gearman_php_extension
发布评论
评论(2)
更改您的代码并使用 GearmanClient::addTask。您可以使用它的返回值来实现监控过程。检查可以在 GearmanTask 类上使用的其他函数。
Change your code and use GearmanClient::addTask. You can use its return value to implement monitoring process. Check on the other functions that you can use on the GearmanTask class.