我在本地系统上使用ubuntu 12.04 64位,PHP 5.3.10,Gearman 1.1.5运行.
以下代码
print gearman_version() . "\n";
抛出php警告
PHP Warning: Module ‘gearman’ already loaded in Unknown on line 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;
}
}
?>
经营工人
php job_entry.php workeradmin
正在等待工作…
运行客户端
php job_entry.php管理员arg1
这给我错误
PHP Error[2]: GearmanClient::doBackground(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed to send server-options packet -> libgearman/connection.cc:430
解决方法:
你可以试试这个吗
$gmworker->addServer('127.0.0.1','4730');