Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Processing a set of resources with a pool of forked children

by shmem (Canon)
on Feb 01, 2014 at 22:02 UTC ( #1072998=note: print w/ replies, xml ) Need Help??


in reply to Processing a set of resources with a pool of forked children

I'd go with IO::Select and IO::Handle (maybe IO::Pipe as well), these modules are core. The following may serve as inspiration.

use IO::Handle; use IO::Select; my %pids; # kid job description keyed on pid my $kids_fh; # pipe filehandles foreach kid keyed on fileno my $rset = IO::Select->new; local $| = 1; local $SIG{CHLD} = sub { my $waitedpid = wait; print "process $waitedpid ($pids{$waitedpid}) finished" if $waited +pid > 0; }; # spawn childeren for my $cmd( @cmds ) { ; my ($rh,$wh); for ($rh,$wh) { $_ = IO::Handle->new; $_->autoflush; } pipe($rh,$wh); if ((my $pid = fork) == 0) { # child code } else { # parent $rset->add($rh); $pids{$pid} = $cmd; $kids_fh{fileno($rh)} = $pid; } } # now interact with kids while(1) { last unless $rset->handles; foreach my $rh (sort { fileno $a <=> fileno $b } $rset->can_read) +{ my $response = do { local $/; <$rh> }; ... ... $rset->remove($rh); } }
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'


Comment on Re: Processing a set of resources with a pool of forked children
Download Code
Re^2: Processing a set of resources with a pool of forked children
by BrowserUk (Pope) on Feb 01, 2014 at 22:04 UTC
    foreach my $rh (sort { fileno $a <=> fileno $b } $rset->can_read) {

    Why sort the ready handles by fileno?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Good catch, thank you. Well, er... first come, first serve. Children always have an eye on each others privileges. ;-)

      Seriously, it's an artifact from cut&paste. In the code I took that from, processing had preferably to be in the same order as the spawned children. Not a technical requirement.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072998]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-08-28 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (261 votes), past polls