Created
October 23, 2012 11:27
-
-
Save phluks/3938283 to your computer and use it in GitHub Desktop.
Perl AnyEvent: How to make async calls in a loop?
This file contains 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/perl | |
use strict; | |
use warnings; | |
use v5.10; | |
use AnyEvent; | |
use AnyEvent::Util; | |
# OBS: This doesn't work! | |
for my $n (1..100) { | |
my $rand = int(rand(10)); | |
my $hdl; | |
$hdl = AnyEvent::io ( | |
fh => \*STDIN, | |
poll => 'r', | |
cb => sub { | |
my $str = `sleep $rand; echo $n`; | |
output($str); | |
} | |
); | |
} | |
# Hmm - args | |
sub output { | |
my $n = shift; | |
say "Got: $n"; | |
} | |
__END__ | |
perl testAny.pl | |
illegal file descriptor or filehandle (either no attached file descriptor or illegal value): at /usr/local/lib64/perl5/AnyEvent/Impl/EV.pm line 55. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment