Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Non-linear sub-routine launching

by MadMax (Novice)
on Apr 04, 2002 at 02:01 UTC ( #156535=perlquestion: print w/replies, xml ) Need Help??
MadMax has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Non-linear sub-routine launching
by tachyon (Chancellor) on Apr 04, 2002 at 02:14 UTC
      One word of warning - spawning too many children too fast can saturate your network so don't write a Denial of Service hack on your own servers!

      Are you sure? I seriously doubt that a server could fork off processes at a rate fast enough to saturate a decent network, assuming that all each process does is send normal pings once every few seconds.
                     s aamecha.s a..a\u$&owag.print

        As you correctly note with a ping of 512 bits (32 bytes each way) I agree it would seem very unlikely that you could spawn processes fast enough to cause problems (even on a narrow 10Mbps network you expect a good 3Mbps before you get collision problems so you would need to spawn over 6,000 processes in a second if they all made a single ping). However once someone discovers the awesome power of fork() it is necessary to consider the potential problems. For instance if you used the default 4 x 32 byte ping instead of a single ping you only need 1,250 odd processes.... I have some vague recollection of bringing a network to a standstill with an ill considered forking script - admittedly each process was sending more data than a simple ping. Another consideration is how many processes you spawn. You certainly can potentially spawn enough to bring a server to a grinding halt.




Re: Non-linear sub-routine launching
by Kanji (Parson) on Apr 04, 2002 at 02:14 UTC
(MeowChow) Re: Non-linear sub-routine launching
by MeowChow (Vicar) on Apr 04, 2002 at 03:12 UTC
    In addition to the fork multi-process model already suggested, you may want to consider using non-blocking sockets with something like POE or Event, both of which are frameworks for writing event-driven programs.
                   s aamecha.s a..a\u$&owag.print
Re: Non-linear sub-routine launching
by shotgunefx (Parson) on Apr 04, 2002 at 03:54 UTC
    For people who don't have fork as an option, another take (that WON'T process in parrallel) but will give you the ability to get errors as soon as one times out is using closures.
    #!/usr/bin/perl -w use strict; use Net::Ping; sub make_ping_iterative (\@) { my $count = 0; my $arrayref = shift; my $p = Net::Ping->new("icmp"); return sub { if ($count >= @$arrayref){ # If you want to keep going for infinity, # Just uncomment the following line and remove the two + that follow # $count = 0 $p->close(); return ; } return ($p->ping($arrayref->[$count], 2), $arrayref->[$cou +nt++]); }; } my @ips = qw (209.6.x.x 209.6.x.x 209.6.x.x ); my $pinger = make_ping_iterative(@ips); while ( my ($up,$host) = $pinger->() ){ print $host, $up ? " is up\n" : " is down\n"; # Do other stuff here. }


    "To be civilized is to deny one's nature."
Re: Non-linear sub-routine launching (boo)
by boo_radley (Parson) on Apr 04, 2002 at 02:16 UTC
    update Translated into spanish since I came in late...

    usted puede estar interesado en el comando de la fork.
    También vea CPAN para los módulos relacionados proceso. Muchos de éstos no trabajan en Windows

Re: Non-linear sub-routine launching
by Dogma (Pilgrim) on Apr 04, 2002 at 06:37 UTC
    Sounds like what you really want is forking which has already been mentioned above. TIMTOWDI - Another option would be to spawn external ping processes (IPC with Open) and then poll for returned results. This is only really usefull if you have to use an external program to ping with. It would save you the cost of one proccess for every external ping process. Take a look at IO::Select

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://156535]
Approved by talexb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2018-06-23 12:24 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.