Created
September 16, 2012 17:28
-
-
Save earino/3733352 to your computer and use it in GitHub Desktop.
First attempt at winner take all parallelism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use 5.16.0; | |
| use warnings; | |
| use strict; | |
| use Parallel::ForkManager; | |
| my @sites = qw/ www.google.com | |
| 4.2.2.2 | |
| 8.8.8.8 | |
| www.yahoo.com | |
| www.apple.com | |
| cnn.com | |
| 127.0.0.1 /; | |
| my $pm = Parallel::ForkManager->new(scalar(@sites), "/tmp"); | |
| my $first_host_to_finish; | |
| $pm->run_on_finish( | |
| sub { | |
| my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $host_ref) = @_; | |
| return if $first_host_to_finish; | |
| $first_host_to_finish = $$host_ref; | |
| } | |
| ); | |
| foreach my $site ( @sites ) { | |
| $pm->start($site) and next; | |
| system("ping -c 1 $site > /dev/null"); | |
| $pm->finish(0, \$site); | |
| } | |
| $pm->wait_all_children; | |
| print "First host: $first_host_to_finish\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment