Skip to content

Instantly share code, notes, and snippets.

@tadzik
Created October 18, 2011 19:02
Show Gist options
  • Save tadzik/1296356 to your computer and use it in GitHub Desktop.
Save tadzik/1296356 to your computer and use it in GitHub Desktop.
Async IO in Perl 6
my $l = IO::Socket::INET.new(
:localhost('localhost'),
:localport(6666),
:listen
);
my @readers;
sub check-readers(@readers is rw) {
my @keepers;
for @readers {
if $_.poll(1, 0.10) {
my $a = $_.recv;
if $a {
print $a;
@keepers.push($_);
} else {
say "something closes";
$_.close;
}
} else {
@keepers.push($_);
}
}
@readers = @keepers;
}
loop {
if $l.poll(1, 1) {
say "can read!";
my $a = $l.accept();
@readers.push($a);
} else {
say "cannot read :(";
}
check-readers(@readers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment