I'm trying to write a very simple multi-threaded spider that can go out and fetch links from a number of pages at once. My basic single threaded script works fine ( everything below the subroutine comment ) when it is it's own script.

However, when I call it as a job from a thread pool, I can't get an response via LWP::UserAgent on my requests. I originally abstracted away the subroutine stuff as it's own package, but that didn't work either ( I thought that might help if there is a scoping error ).

Given that I have no experience with multithreading over than playing around with a few of the modules and making threads print stuff concurently, am I doing something mind-numbingly stupid or is there something worse going on?

Thanks for advice in advance.
#!c:/usr/bin/perl # CPAN modules use Thread::Pool; use LWP::UserAgent; use HTML::LinkExtor; use URI::URL; $pool = Thread::Pool->new( { optimize => 'cpu', # default: memory do => 'XXX', # must have checkpoint => \&checkpoint, frequency => 1000, workers => 5, # default: 1 maxjobs => 50, # default: 5 * workers minjobs => 5, # default: maxjobs / 2 }, ); # Start up some threads $pool->job( "http://www.yahoo.com" ); $pool->job( "http://www.eroticfalconry.com" ); $pool->join; # wait for all threads to finish #### subroutines sub XXX { $current_url = @_[0]; @queue = (); while( $current_url ne "" ){ @links = get("$current_url" ); print "links in url\n********************\n"; print join("\n", @links), "\n"; push(@queue,@links); #print "links in queue\n********************\n"; #print join("\n", @queue), "\n"; $current_url = pop(@queue); print ">>>>new url=$current_url\n"; #this is where we go next } } my $p = HTML::LinkExtor->new(\&callback); my @links = (); # Set up a callback that collect links sub callback { my($tag, %attr) = @_; return if $tag ne 'a'; # we only look closer at <a ...> push(@links, values %attr); } sub get { $url = "@_[0]"; print "Entering html::getlinks, url=$url\n"; my $ua = LWP::UserAgent->new; $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->pars +e($_[0])}); $base = $res->base; # Expand all URLs to absolute ones @links = map { $_ = url($_, $base)->abs; } @links; return @links; } $version = 1;

In reply to Threads and LWP::UserAgent question by gatito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.