跨多种环境部署 Gearman
2010-10-09 08:13:21 来源:WEB开发网清单 9 展示了修改后的工人脚本。
清单 9. 基于 memcached 的工人
use Cache::Memcached;
use Gearman::Worker;
my $cache = new Cache::Memcached {
'servers' => [
'192.168.0.2:11211',
],
};
my $worker = Gearman::Worker->new;
$worker->job_servers('192.168.0.2:4730');
$worker->register_function('wordcount' => \&wordcount);
$worker->work while 1;
sub wordcount
{
my ($arg) = @_;
my $id = $arg->arg;
print STDERR "Providing word count for ",$id,"\n";
my $string = $cache->get(sprintf('doc-%d-srctxt',$id));
if (!defined($string))
{
$cache->set(sprintf('doc-%d-status',$id),
'Error: Source text not found');
return;
}
my @words = split /\s+/,$string;
$cache->set(sprintf('doc-%d-status',$id),
'Complete');
$cache->set(sprintf('doc-%d-result',$id),
scalar @words);
}
注意,在工人中,脚本使用 status 和 tagged memcached 项来保留信息;失败可以被写入 status。这允许客户机在发生暂时的失败时重新提交工作请求。
更多精彩
赞助商链接