in reply to A question of fork efficiency
This seems like a similar problem to Optimized remote ping in particular Re: Optimized remote ping which was something I did before joining perlmonks.
It essentially does what an async framework would do (all in one process).
Here's a slightly updated version:
#!/usr/bin/perl # http://perlmonks.org/?node_id=1135109 # http://perlmonks.org/?node_id=11103970 use strict; use warnings; use IO::Socket; use IO::Select; my @ips = map "192.168.1.$_", 1..20; # your IPs here my $port = 22; # your port here my $max = 500; # somewhat smaller than max "open files" my %handles; my $sel = IO::Select->new; while( @ips or $sel->count ) { if( @ips and $sel->count < $max ) { my $ip = shift @ips; my $fh = IO::Socket::INET->new(PeerHost => $ip, PeerPort => $port, Proto => 'tcp', Blocking => 0); $handles{$fh} = "$ip:$port"; $sel->add($fh); } elsif( @ips ? $sel->count >= $max : $sel->count ) { for my $fh ( $sel->can_write ) { print $fh->connected ? ' ' : 'not', " alive $handles{$fh}\n"; $sel->remove($fh); delete $handles{$fh}; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: A question of fork efficiency
by synless (Acolyte) on Aug 06, 2019 at 18:43 UTC | |
by tybalt89 (Monsignor) on Aug 06, 2019 at 19:51 UTC | |
by trippledubs (Deacon) on Aug 07, 2019 at 13:02 UTC | |
by holli (Abbot) on Aug 07, 2019 at 13:57 UTC | |
by synless (Acolyte) on Aug 07, 2019 at 14:43 UTC | |
by synless (Acolyte) on Aug 07, 2019 at 13:23 UTC | |
by tybalt89 (Monsignor) on Aug 07, 2019 at 15:40 UTC | |
by synless (Acolyte) on Aug 08, 2019 at 16:04 UTC | |
by synless (Acolyte) on Aug 07, 2019 at 14:49 UTC | |
by tybalt89 (Monsignor) on Aug 07, 2019 at 15:20 UTC | |
by synless (Acolyte) on Aug 07, 2019 at 18:03 UTC | |
| |
by synless (Acolyte) on Aug 07, 2019 at 15:58 UTC |