Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I belive BrowserUk originally wrote this code - I could not find his original post, so I'm sharing again.

This uses threads/queues, and provides the functionality you are seeking, aside from being an excellent reference implementation:

#! perl -slw use strict; use threads; use Thread::Queue; $| = 1; #$OUTPUT_AUTOFLUSH our $KIDS ||= 10; our $WORK ||= 500; our $SLEEP||= 5; sub kid { my( $Q ) = shift; my $tid = threads->tid; my $count=0; printf "Kid: %02d started\n", $tid; ## Pick a work item of the queue and process it while( my $work = $Q->dequeue ) { printf "Kid: %02d processing work item '%s'\n", $tid, $work; $count ++; ## Replace the sleep with the code to process teh work items rand > 0.7 and sleep rand( $SLEEP ); } print "kid: $tid ending after processing $count items.\n"; } ## A queue for communications my $Q = new Thread::Queue; ## Start the kids my @kids = map{ threads->create( \&kid, $Q ) } 1 .. $KIDS; ## Wait till they're all up and running sleep 1 until @{[ threads->list ]} == $KIDS; ## Feed the queue with work ## The limit just ensure we don't fill lots of memory for my $workitem ( 1 .. $WORK ) { sleep 1 while $Q->pending > $KIDS *10; print "Queueing work item $workitem"; $Q->enqueue( $workitem ); } ## Tell them to stop $Q->enqueue( (undef) x $KIDS ); ## And wait for them to do so. $_->join for @kids;

        This is not an optical illusion, it just looks like one.


In reply to Re: Fast provider feeding slow consumer by NetWallah
in thread Fast provider feeding slow consumer by leostereo

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?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found