Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Running subroutines asynchronously

( #156507=categorized question: print w/ replies, xml ) Need Help??
Contributed by MadMax on Apr 04, 2002 at 00:24 UTC
Q&A  > subroutines


Description:

Is there a way to run a sub-routine in a non-linear fashion? For example, I need to ping to 100 ip's to check the connectivity of my entire system.

(linear method) I currently ping each ip individually "one at a time" and wait for the entire list to complete. This can take a while if I set a 2 second timeout on the ping.

(non-linear method) What I would rather do is use a subroutine to ping each ip "in the background (in unix: a command followed by a '&')" and then wait until each of the ping subroutines reports back (or an appropriate amount of time) and compile the results.

Answer: Running subroutines asynchronously
contributed by crazyinsomniac

use Proc::Forkfunc;
use strict;

$|++; # autoflush/unbuffer

my @child_args = qw(1 2 );

forkfunc(\&child_func, @child_args);
forkfunc(\&child_func, 3);
forkfunc(\&child_func, 4);
forkfunc(\&child_func, 5);

sub child_func {
    # sleep for rand(3)*rand(3)
    select undef, undef, undef,  rand(3) * rand(3);

    print shift(@_);
    print "\n";
}
Proc::Forkfunc has the annoying habit of printing to STDERR "call to child returned", but it's not a complex module, and if you crack it open (look at the source), you can easily figure out what's goin on. You should also take a look at perlfork. This will work for most systems, but remember, forking is experimental on Win32 machines, and is not available before v5.6, but there is an alternative, Win32::Process.
Answer: Running subroutines asynchronously
contributed by NetWallah

Here is some Thread::Queue based code that demonstrates another way to resolve the parallel-ping problem.

You can use it to model generic parallel processing subroutines where you have one work queue, and multiple workers. The results generated by the workers is fed back into a single queue, that is read by the main process.

Answer: Running subroutines asynchronously
contributed by ehdonhon

Take a look at Parallel::ForkManager. I have actually used it in the past to solve that very problem.

Answer: Running subroutines asynchronously
contributed by tachyon

Sounds like a job for fork(). Fork a stack of kids to do the pinging and waiting in parallel.

Check out Non-linear sub-routine launching for more details.

#!/usr/bin/perl -w use strict; use POSIX ":sys_wait_h"; $|++; my @hosts = qw( www.perlmonks.com www.perl.com www.netscape.com ); my (@pids, $count); for my $host(@hosts) { sleep 1; # this limits to 1 kid per second, not actually required $count++; my $pid = fork(); push @pids , $pid; die "Fork failed\n" unless defined $pid; next if $pid; # get parent to reiterate and fork more kids my $reply = `ping -n 1 $host`; # get kids pinging, single ping print "I am child $count, pinged $host\n$reply\n\n"; exit; # kill child } # wait for kids to finish, no zombies on us my $kids; do { $kids = waitpid(-1,&WNOHANG); } until $kids == -1; print "Spawned $count kids, waited on @pids\nAll done!";

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (5)
    As of 2014-10-23 05:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (124 votes), past polls