Created
July 10, 2012 20:44
-
-
Save fvox/3086129 to your computer and use it in GitHub Desktop.
fork_call problem with ipc [fix'd]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use common::sense; | |
use LWP::Simple; | |
use AnyEvent::Util; | |
use HTML::LinkExtor; | |
use Data::Printer colored => 1; | |
sub extract_links { | |
my @ret = (); | |
my $parser = new HTML::LinkExtor( | |
sub { | |
my ($tag, %links) = @_; | |
return if $tag ne 'a'; | |
my ($url) = values %links; | |
push @ret, $url; | |
} | |
); | |
$parser->parse(get($_[0])); | |
return \@ret; | |
} | |
my $urls = [ | |
qw( | |
http://terra.com.br/ | |
http://g1.com.br/ | |
http://r7.com.br/ | |
http://uol.com.br/ | |
) | |
]; | |
my @pool = (); | |
my $cv = AnyEvent->condvar; | |
for my $url (@{$urls}) { | |
$cv->begin; | |
fork_call { | |
my $links = extract_links($url); | |
return $links; | |
} | |
sub { | |
push @pool, @{$_[0]}; | |
$cv->end; | |
}; | |
} | |
$cv->wait; | |
say 'POOL: ' . p(@pool); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment