基本上,我想要:
- 从网络中读取大量数据到内存中的数组中。
- 异步地将此数组数据写入磁盘之前通过bzip2运行它。
重复执行..
这可行吗?如果可以,我知道我必须以某种方式将下一次数据传递到不同的数组中,因为AIO文档说在异步写入完成之前不得更改此数组。我希望将所有写入磁盘的操作都后台处理,以便bzip2处理比网络读取时间更长。
这可行吗?以下是我认为所需的简单示例,但这只是为了测试而将文件读入数组@a中。
use warnings;
use strict;
use EV;
use IO::AIO;
use Compress::Bzip2;
use FileHandle;
use Fcntl;
my @a;
print "loading to array...\n";
while(<>) {
$a[$. - 1] = $_;
}
print "array loaded...\n";
my $aio_w = EV::io IO::AIO::poll_fileno, EV::WRITE, \&IO::AIO::poll_cb;
aio_open "./out", O_WRONLY || O_NONBLOCK, 0, sub {
my $fh = shift or die "error while opening: $!\n";
aio_write $fh, undef, undef, $a, -1, sub {
$_[0] > 0 or die "error: $!\n";
EV::unloop;
};
};
EV::loop EV::LOOP_NONBLOCK;
aio_write
语句中的标量$a
是一个不同于保存输入的数组@a
的变量。 - mob