Gearman For Perl



 

Gearman perl 实践

环境:3台Linux主机

软件:gearman-0.14.tar.gz

 

Client:安装gearman::client

Work:安装 gearman::worker

Job 安装 gearman-0.14.tar.gz

 

Client端代:

use Gearman::Client;

my $client = Gearman::Client->new;

$client->job_servers(’192.168.0.2:4730′);

my $result = $client->do_task(‘wordcount’,’the quick brown fox jumps over the lazy dog’);

print “Words $$result\n”;

Worker端代码:

use Gearman::Worker;

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 ($input) = @_;

my @words = split /\s+/,$input->arg;

return (scalar @words);

}

Job端开启gearman的服务

Gearmand –d –u root

详细信息:

Gearmand –vvv

 

 

Perl 进度条

 #!/usr/bin/perl -w
 $n = 27;
 for($i=1;$i<=$n;$i++){
 proc_bar($i,$n);
 select(undef, undef, undef, 0.2);
 }
 print "\n";

 sub proc_bar{
 local $| = 1;
 my $i = $_[0] || return 0;
 my $n = $_[1] || return 0;
 print "\r33[36m[33[33m".("|" x int(($i/$n)*50)).
 (" " x (50 - int(($i/$n)*50)))."33[36m]";
 printf("%2.1f%%33[0m",$i/$n*100);
 local $| = 0;
 }

Perl 简单文件操作

一、打开、关闭文件
  语法为open (filevar, filename),其中filevar为文件句柄,或者说是程序中用来代表某文件的代号,filename为文件名,其路径可为相对路径,亦可为绝对路径。
    open(FILE1,"file1");
    open(FILE1, "/u/jqpublic/file1");
  打开文件时必须决定访问模式,在PERL中有三种访问模式:读、写和添加。后两种模式的区别在于写模式将原文件覆盖,原有内容丢失,形式为:open (outfile,">outfile");而添加模式则在原文件的末尾处继续添加内容,形式为:open(appendfile, ">>appendfile")。要注意的是,不能对文件同时进行读和写/添加操作。
  open的返回值用来确定打开文件的操作是否成功,当其成功时返回非零值,失败时返回零,因此可以如下判断:
    if (open(MYFILE, "myfile")) {
    # here’s what to do if the file opened successfully
    }
  当文件打开失败时结束程序:
    unless (open (MYFILE, "file1")) {
    die ("cannot open input file file1\n");
    }
  亦可用逻辑或操作符表示如下:
    open (MYFILE, "file1") || die ("Could not open file");
  当文件操作完毕后,用close(MYFILE); 关闭文件。
二、读文件
  语句$line = <MYFILE>;从文件中读取一行数据存储到简单变量$line中并把文件指针向后移动一行。<STDIN>为标准输入文件,通常为键盘输入,不需要打开。
  语句@array = <MYFILE>;把文件的全部内容读入数组@array,文件的每一行(含回车符)为@array的一个元素。
三、写文件
  形式为:
    open(OUTFILE, ">outfile");
    print OUTFILE ("Here is an output line.\n");
  注:STDOUT、STDERR为标准输出和标准错误文件,通常为屏幕,且不需要打开。