Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: A question of fork efficiency

by holli (Abbot)
on Aug 07, 2019 at 21:11 UTC ( [id://11104117]=note: print w/replies, xml ) Need Help??


in reply to A question of fork efficiency

Is this modern enough for you? ;-)
use v6; port_check( ['perlmonks.com', 'fail.com', 'perlmonks.org'], 22 ); sub port_check( @nodes, $port ) { my @promises = @nodes.map( -> $host { IO::Socket::Async.connect( $host, $port ) }); await Promise.allof( @promises ); for @nodes Z @promises -> [$node, $promise] { if $promise.status ~~ Kept { given $promise.result { my $peer = "{.peer-host}:{.peer-port}"; my $socket = "{.socket-host}:{.socket-port}"; say "$socket connected to $peer"; .close; } } else { say "not connected to $node:$port"; } } }
D:\ENV>perl6 pcheck.pl 192.168.178.20:49859 connected to 216.92.34.251:22 not connected to fail.com:22 192.168.178.20:49861 connected to 209.197.123.153:22
Edit: This is the same code with a custom timeout.
use v6; port_check( ['perlmonks.com', 'fail.com', 'fail.com', 'fail.com', 'fai +l.com', 'fail.com', 'fail.com', 'fail.com', 'perlmonks.org'], 55, 2 ) +; sub port_check( @nodes, $port = 22, $timeout = 10) { my @promises = @nodes.map( -> $host { IO::Socket::Async.connect( $host, $port ); }); my @waits = @promises.map( -> $promise { Promise.anyof( Promise.in( $timeout ), $promise ) }); await Promise.allof( @waits ); for @nodes Z @promises -> [$node, $promise] { if $promise.status ~~ Kept { given $promise.result { my $peer = "{.peer-host}:{.peer-port}"; my $socket = "{.socket-host}:{.socket-port}"; say "$socket connected to $peer"; .close; } } else { say "not connected to $node:$port"; } } }


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: A question of fork efficiency
by synless (Acolyte) on Aug 08, 2019 at 16:10 UTC

    Using v6 around here would be nice but unfortunately that currently isn't possible. I'll definitely keep this in mind for the future though if that ever becomes possible. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-28 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found