in reply to Perl crashing with Parallel::ForkManager and WWW::Mechanize
Windows & fork are like windows & stones; mix them and some thing's gonna break :)
Try this (slightly tested):
#! perl -slw use strict; use threads; use threads::shared; use LWP::Simple; use HTML::TreeBuilder::XPath; sub locked(\$) :lvalue { lock ${$_[0]}; ${$_[0]} } our $T //= 3; my $stdoutSem :shared; my $running :shared = 0; while( my $url = <> ) { chomp $url; async { ++locked( $running ); if( my $content = get $url ) { my $tree = HTML::TreeBuilder::XPath->new(); $tree->parse( $content ); # do some processing here on the content if( my $title = $tree->findnodes( '/html/head/title' ) ) { chomp $title; lock $stdoutSem; print "$url : $title "; } # once done then delete the root node $tree->delete(); } --locked( $running ); }->detach; Win32::Sleep 500 while $running >= $T; }
And use it like this:
thisScript.pl urls.list > output.file
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl crashing with Parallel::ForkManager and WWW::Mechanize
by NeonFlash (Novice) on Aug 05, 2012 at 15:29 UTC | |
by BrowserUk (Patriarch) on Aug 05, 2012 at 15:41 UTC | |
by NeonFlash (Novice) on Aug 06, 2012 at 07:24 UTC |