【发布时间】:2011-02-06 20:06:37
【问题描述】:
基本上我想:
- 将大量数据从网络中读入数组到内存中。
- 异步写入此数组数据,在它到达磁盘之前通过 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不同。 -
如果您正在写入 bzip 进行压缩,您甚至不需要 AIO。打开到 bzip 的管道,然后从套接字(异步)读取并将该数据写入 bzip 管道。 AnyEvent::Handle 就是你所需要的。
标签: perl asynchronous io