Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
So, I wanted to get some monkly feedback on whether this makes sense or not, whether it seems like a reasonable approach to my problem. This is my first incursion into the beautiful work of threads and everything seems to work as intended but hey, peer review wouldn't hurt.

I wrote a subroutine that uses WWW::Mechanize to simulate user activity on a few pages / web forms. I have an input file with a few thousand records that passes parameters to the said subroutine, but I wanted to have 5 concurrent sessions and no more than 5. After doing a little reading on-line, it seemed like threads was a good candidate for this type of scenario so I wrote the following test script as a proof of concept.

It seems to work well but as they say, there's always more than one way of doing things. Oh, and I'm running ActiveState Perl 5.8.8 (build 822) on Windows XP.

Any takers?
#!/usr/bin/perl -w use strict; use threads; my $maxThreads = 5; open(LOGGER,'>>logfile') or die(); for my $num (1..50) { print "Starting thread # $num\n"; my $thread = async { &doStuff($num); }; while (threads->list(threads::running) > $maxThreads) { for my $thread (threads->list(threads::all)) { if ($thread->is_joinable()) { print $thread->join(); } } } } print "Waiting for the last ". threads->list(threads::running) ." thre +ads to finish\n"; while (threads->list(threads::all)) { for my $thread (threads->list(threads::joinable)) { print $thread->join(); } } close LOGGER; sleep(1); sub doStuff { my $num = shift; my $secs = int(rand(3)); print LOGGER "Thread # $num is napping for $secs seconds.\n"; sleep $secs; print LOGGER "Thread # $num is awake.\n"; return "This returned from thread # $num\n"; }

In reply to Controlling Thread Numbers by BBQ

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-02-03 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?