繁体   English   中英

并行运行2个WWW :: Mechanize :: Firefox副本

[英]running 2 copies of WWW::Mechanize::Firefox in parallel

我正在运行草莓窗户perl。 我有两份安装了mozrepl的firefox Portable。

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

让我知道您是否需要更多信息。 我已经花了很多时间,无法解决。

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

亲切的问候::罗布

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM