并行运行2份WWW :: Mechanize :: Firefox

时间:2015-03-26 13:15:52

标签: perl parallel-processing www-mechanize

我正在运行草莓窗口perl。我有两个firefox便携版的副本 安装了mozrepl。

C:\\FirefoxPortable1\\FirefoxPortable.exe'; Using port 4241
C:\\FirefoxPortable2\\FirefoxPortable.exe'; Using port 4242

我想使用这两个浏览器并行浏览。我有个好的开始:

use strict;
use threads;
use Thread::Semaphore;
use threads::shared;
use WWW::Mechanize::Firefox;

my @list :shared;
my @browsers :shared;

@list = (10,20); # ,30,40,50,60,70,80,90,100
@browsers = (1,2); # Array of browsers

my $sem = Thread::Semaphore->new(2);

my @threads;
while (@list != 0) {
  $sem->down; # Request a thread slot, wait if non are available
  my $param1 = shift @list;
  my $param2 = shift @browsers;
  my @params = ($param1, $param2);
  push @threads, threads->create(\&mySubName, @params);
}
$_->join for @threads;

sub mySubName {
  my $id = shift;
  my $browserId = shift;

  print "Running for List Number: $id with browser $browserId.\n";

  my $host = 'C:\\FirefoxPortable'. $browserId .'\\FirefoxPortable.exe';
  my $port = 'localhost:424' . $browserId; 
  my $ff = Firefox::Application->new(
           autodie => 0,
           launch => [$host],
           repl => $port,
           );

  my $mech;
  eval{ $mech = WWW::Mechanize::Firefox->new(
            app => $host,
            repl => $port,
            ); };
  if ($@) {
    undef $mech;
    undef $ff;
    push @list, $id; # on error add the store back into the list
    $sem->up; # Release slot
    return;
  }

  $mech->allow(javascript => 1);

  eval { $mech->get('www.google.com'); };
  if ($@) {
    undef $mech;
    undef $ff;
    push @list, $id; # on error add the store back into the list
    $sem->up; # Release slot
    return;
  }

  #undef $mech;
  #undef $ff;
  push @browsers, $browserId;
  $sem->up # Release slot
}

我认为我非常接近它。我可以让它与一个人合作。因此,如果您将信号量限制为1(并将浏览器列表更改为仅一个),则运行正常。我认为我的问题与启动Firefox::ApplicationWWW::Mechanize::Firefox

时使用的参数有关

我得到的错误:

Thread 2 terminated abnormally: Failed to connect to host localhost port 4242, problem connecting to "localhost", port 4242: No connection could be made because the target machine actively refused it. at C:/strawberry/perl/site/lib/MozRepl/Client.pm line 144

如果您需要更多信息,请与我们联系。我花了很多时间在这上面,无法弄明白。

1 个答案:

答案 0 :(得分:0)

实现目标的一种实用方法是使用Selenium设置浏览器。

亲切的问候:: Rob