如何在Perl中使用Distributed :: Process框架

时间:2014-06-04 13:56:56

标签: perl

我正在尝试在Perl中使用Distributed::Process框架。我已经在我的机器上成功实现了它。但我无法弄清楚如何使用它来运行/测试示例脚本。我尝试在#synopsis

中运行代码清单

任何人都可以通过提供示例脚本以及逐步测试来帮助我。

我想在所有工作机器的文件中打印'Hello World'....我修改了上面链接中提供的3个.pl文件,如下所示。

my_worker.pl

package MyWorker;
use Distributed::Process;
use Distributed::Process::Worker;

sub run {
    my $self = shift;

    # do interesting stuff
   open (MYFILE, '>>data.txt');
   print MYFILE "Hello World\n";
   close (MYFILE);

    # report about what happened
    $self->result("logs the results");
}

my_server.pl

# Server
use Distributed::Process;
use Distributed::Process::Server;
use Distributed::Process::Master;
use MyWorker;

$master = new Distributed::Process::Master
    -in_handle => STDIN, -out_handle => STDOUT,
    -worker_class => 'MyWorker',
    -n_workers => 2;
$server = new Distributed::Process::Server -port => 8147, -master => $master;

$server->listen();

my_client.pl

# Client
use Distributed::Process;
use Distributed::Process::Client;
use MyWorker;

$client = new Distributed::Process::Client -worker_class => 'MyWorker',
    -host => localhost, -port => 8147;
$client->run();

然后尝试使用perl my_worker/server/client.pl(在不同的终端)运行它。 my_server和客户端抛出错误如下:

Can't locate MyWorker.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at my_server.pl line 5.
BEGIN failed--compilation aborted at my_server.pl line 5.

我不知道如何/在何处指定主机名或运行它的正确过程

1 个答案:

答案 0 :(得分:1)

您收到该错误是因为use MyWorker;语句无法找到package文件。

基本上,您需要将文件my_worker.pl重命名为MyWorker.pm

此外,您应该在每个perl脚本的顶部包含use strict;use warnings。这并不直接触及您的问题,但如果您现在不开始这样做,它就会出现。