在perl中异步写入文件

Ste*_*hen 8 io perl asynchronous

基本上我想:

  1. 将大量数据从网络读入阵列到内存中.
  2. 异步写入此数组数据,在它到达磁盘之前通过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;
Run Code Online (Sandbox Code Playgroud)

Nic*_*nes 0

您可能对Perlbal如何处理此类操作感兴趣。我相信它使用Danga::Socket来完成与您想要做的非常相似的事情。