本文共 2642 字,大约阅读时间需要 8 分钟。
$Id$
版权 © 2011, 2012, 2013 http://netkiller.github.io
$Date$
摘要
在群里看到有网友问,IDC的服务器是否需要开启防火墙,我意识到应该写一篇关于IDC安全的文章。
我的系列文档
目录
文本格式回复
yum install gearmand
PHP扩展安装
#!/bin/bashyum install libgearman-devel -ypecl install gearmancat >> /srv/php/etc/conf.d/gearman.ini <
确认模块是否安装,同时检查gearman扩展的版本。
# php -m | grep gearmangearman# php -r 'printf("%s\r\n",gearman_version());'1.1.8
测试脚本 server.php
addServer();$worker->addFunction("reverse", "my_reverse_function");while ($worker->work());function my_reverse_function($job){ return strrev($job->workload());}?>
测试脚本 client.php
addServer();print $client->doNormal("reverse", "Hello World!");?>
我用'o' 表示与上次备份中有差异的部分。
Gearman 向functon 传递参数只能通过$job->workload(), 而 $job->workload() 只能传递字符串。
如果托传递多个参数,需要将参数序列化后传递
返回值也一样,一个字符串可以直接返回,如果返回数字类型是不允许的,需要序列化处理
例 1. 多参数传递与返回值实例
Server
register();$config = new Configuration();$connectionParams = array( 'dbname' => 'example', 'user' => 'www', 'password' => 'password', 'host' => '192.168.2.1', 'driver' => 'pdo_mysql',);$conn = DriverManager::getConnection($connectionParams, $config);$host = '127.0.0.1';$port = 4730;echo "Starting\n";# Create our worker object.$gmworker= new GearmanWorker();# Add default server (localhost).$gmworker->addServer($host, $port);# Register function "reverse" with the server. Change the worker function to# "reverse_fn_fast" for a faster worker with no output.$gmworker->addFunction("members", "members");print "Waiting for job...\n";while($gmworker->work()){ if ($gmworker->returnCode() != GEARMAN_SUCCESS) { echo "return_code: " . $gmworker->returnCode() . "\n"; break; }}function members($job){ global $conn; $param = unserialize($job->workload()); print_r($param); $sql = "SELECT username FROM members limit ".$param['limit'].",".$param['offset']; $stmt = $conn->query($sql); while ($row = $stmt->fetch()) { //printf("%s\r\n", ); $result[] = $row['username']; } return serialize($result);}
Client
addServer();# run reverse client in the background$job_handle = $gmclient->doNormal("members",serialize(array('limit'=>5,'offset'=>10)));if ($gmclient->returnCode() == GEARMAN_SUCCESS){ print_r(unserialize($job_handle));}
运行结果
$ php client.php Array( [0] => 257000005 [1] => 257000006 [2] => 257000009 [3] => 257000010 [4] => 257000011 [5] => 257000012 [6] => 257000013 [7] => 257000014 [8] => 257000015 [9] => 257000016)
转载地址:http://rmovx.baihongyu.com/