Skip to content

Instantly share code, notes, and snippets.

@phluks
Created October 23, 2012 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phluks/3938283 to your computer and use it in GitHub Desktop.
Save phluks/3938283 to your computer and use it in GitHub Desktop.
Perl AnyEvent: How to make async calls in a loop?
#!/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