Created
October 18, 2011 19:02
-
-
Save tadzik/1296356 to your computer and use it in GitHub Desktop.
Async IO in Perl 6
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
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