Hello Monks, I am working in a project which requires me to place 250 parallel HTTP request and get its response and do some action based on the response. I created 250 threads and let each thread handle one request and also the logic to do something based on the incoming response. Now my problem is my application has limitation of 250 request, which means at any point of time there should be only 250 threads in operation , let say if 10 out of those 250 threads have completed their operation and exited, I need to replace those 10 threads with new urls for new request to be made. In other words there should be 250 threads operating at any point of time.Is there anyway to keep in track of the number of thread in operation.And create corresponding number of new threads as and when threads exit , keeping the sum total of number threads in operation to be constant 250 at all times. please suggest me modification to the code.. Thanks in advance.. The code am currently using...
use strict; use LWP::UserAgent; use HTTP::Request; use threads; my $url="http://www.google.com"; my %hash; my $dthread; my %shash; open FH,">result.txt"; my $numworkers=1000; while($numworkers) { foreach my $dthread(1..250) { $hash{$dthread} = threads->create(\&requester,$url); } foreach my $dthread(1..250) { $hash{$dthread}->join(); $numworkers--; } } } sub requester() { my $url1=shift; my $request = HTTP::Request->new(GET => $url1); my $ua = LWP::UserAgent->new; print "Requesting Header....\n\n"; my $response = $ua->request($request); print "Requesting placed....\n\n"; my $content=$response->headers_as_string; print FH "$content\n\n"; print " .........thread completed............\n\n"; }

In reply to Newbie question in threading by karthick

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.