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

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

by shmem (Chancellor)
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'

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072998]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-02-21 07:35 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (275 votes). Check out past polls.