Skip to content

Instantly share code, notes, and snippets.

@mash
Created October 22, 2009 10:07
Show Gist options
  • Save mash/215873 to your computer and use it in GitHub Desktop.
Save mash/215873 to your computer and use it in GitHub Desktop.
use AnyEvent::AIO;
use IO::AIO;
use Fcntl;
warn "[pre]";
my $num = 0;
my $max = 9;
for $num (0..$max) {
async_append( "temp.txt", "$num\n" );
}
warn "[aft]";
my $cv = AnyEvent->condvar;
sub async_append {
my ($file, $text) = @_;
my $fh = aio_open(
$file, O_WRONLY|O_CREAT|O_APPEND, 0644,
sub {
my $fh = shift
or die "open $file failed: $!";
warn "[opened]$text";
my $ret = aio_write(
$fh, 0, length($text), $text, 0,
sub {
$_[0] or die "couldnt write $text: $!";
warn "[wrote]$text";
aio_close(
$fh,
sub {
warn "[closed]$max:$text";
$cv->send if $max==0;
$max--;
}
);
});
}
);
}
$cv->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment