我在本地系统上运行 ubuntu 12.04 64 位,PHP 5.3.10,gearman 1.1.5。以下代码
print gearman_version() . "\n";
抛出 php 警告
PHP 警告:模块 'gearman' 已加载到第 0 行的未知中
我还使用构建了 yii 控制台应用程序 WorkerAdminCommand.php
<?php
class WorkerAdminCommand extends CConsoleCommand {
public function run() {
$gmworker = new GearmanWorker();
$gmworker->addServer(); // also tried with $gmworker->addServer('127.0.0.1:4730');
$gmworker->addFunction("getMyFunc", array($this, "getMyFunc"));
print "Waiting for job...\n";
while ($gmworker->work()) {
if ($gmworker->returnCode() != GEARMAN_SUCCESS) {
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
}
public function getMyFunc($job) {
echo "start \n";
// long task
echo "\n end \n";
}
} ?>
和 AdminCommand.php
<?php
class AdminCommand extends CConsoleCommand {
public function run($args) {
$gmclient = new GearmanClient();
$gmclient->addServer();
echo "Sending job\n";
$data = $args[0];
$result = $gmclient->doBackground("getMyFunc", $data);
# Check for various return packets and errors.
switch ($gmclient->returnCode()) {
case GEARMAN_WORK_STATUS:
list($numerator, $denominator) = $gmclient->doStatus();
echo "Status: $numerator/$denominator complete\n";
break;
case GEARMAN_WORK_FAIL:
echo "Failed\n";
exit;
case GEARMAN_SUCCESS:
echo "Job process successfully\n";
break;
default:
echo "RET: " . $gmclient->returnCode() . "\n";
exit;
}
echo $result . PHP_EOL;
}
}
?>
运行 worker php job_entry.php workeradmin 等待工作...运行客户端 php job_entry.php admin arg1 这给了我错误
PHP 错误 [2]: GearmanClient::doBackground(): send_packet(GEARMAN_COULD_NOT_CONNECT) 发送服务器选项包失败 -> libgearman/connection.cc:430