Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm trying to understand the Thread module better. I'm working on a test script that uses 5 worker threads at a time to download web content from 10 URLs. The first 5 threads start fine, but then after the first URL is retrieved the program hangs.

I've tried setting it up several different ways, but I'm just not getting anything that works yet.

I have been able to successfully write a script that just starts one thread per URL that gets all the sites content in parallel, but I want to be able to limit the number of threads running at any given time in order to keep the script from sucking up all the bandwidth and CPU time as the list of URLs grows.

Here is my non-working example:

use Thread; use Thread::Queue; use LWP::Simple; use strict; my $q = new Thread::Queue; $q->enqueue qw( ); while ($q->pending) { my $kid; my @threads = Thread->list; my $current = scalar(@threads); if ($current < 6) { my $url = $q->dequeue; print "Retrieving $url\n"; $kid = new Thread(\&get_url, $url); } } $q->enqueue(undef); # Don't know if I even need this part; it has no effect at this point for (Thread->list) { if ($_->tid && !Thread::equal($_, Thread->self)) { $_->join; } } sub get_url { my $url = shift; get($url); print "Retrieved $url\n"; }

In reply to Threading by httptech

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others rifling through the Monastery: (1)
    As of 2024-07-19 02:22 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.