我可以在Perl中打开的文件句柄数量是否有限制?

Ale*_*lds 2 io perl file-io

我正在设置包含文件句柄的哈希引用.

我的输入文件的第四列包含一个标识符字段,我用它来命名文件句柄的目标:

col1    col2    col3    id-0008    col5
col1    col2    col3    id-0002    col5
col1    col2    col3    id-0001    col5
col1    col2    col3    id-0001    col5
col1    col2    col3    id-0007    col5
...
col1    col2    col3    id-0003    col5
Run Code Online (Sandbox Code Playgroud)

我使用GNU核心实用程序来获取标识符列表:

$ cut -f4 myFile | sort | uniq
id-0001
id-0002
...
Run Code Online (Sandbox Code Playgroud)

此列中可以有超过1024个唯一标识符,我需要为每个标识符打开一个文件句柄,并将该句柄放入哈希引用中.

my $fhsRef;
my $fileOfInterest = "/foo/bar/fileOfInterest.txt";

openFileHandles($fileOfInterest);
closeFileHandles();

sub openFileHandles {                                                                                                                                                                                                              
    my ($fn) = @_;                                                                                                                                                                                                              

    print STDERR "getting set names... (this may take a few moments)\n";                                                                                                                                                           
    my $resultStr = `cut -f4 $fn | sort | uniq`;                                                                                                                                                                 
    chomp($resultStr);                                                                                                                                                                                                             
    my @setNames = split("\n", $resultStr);                                                                                                                                                                                        

    foreach my $setName (@setNames) {                                                                                                                                                                                              
        my $destDir = "$rootDir/$subDir/$setName"; if (! -d $destDir) { mkpath $destDir; }                                                                                                                                          
        my $destFn = "$destDir/coordinates.bed";                                                                                                                                                                                   
        local *FILE;                                                                                                                                                                                                               
        print STDERR "opening handle to: $destFn\n";                                                                                                                                                                               
        open (FILE, "> $destFn") or die "could not open handle to $destFn\n$!\n";                                                                                                                                                  
        $fhsRef->{$setName}->{fh} = *FILE;                                                                                                                                                                                         
        $fhsRef->{$setName}->{fn} = $destFn;                                                                                                                                                                                       
    }                                                                                                                                                                                                                              
}                                                                                                                                                                                                                                  

sub closeFileHandles {                                                                                                                                                                                                             
    foreach my $setName (keys %{$fhsRef}) {                                                                                                                                                                                        
        print STDERR "closing handle to: ".$fhsRef->{$setName}->{fn}."\n";                                                                                                                                                         
        close $fhsRef->{$setName}->{fh};                                                                                                                                                                                           
    }                                                                                                                                                                                                                              
}       
Run Code Online (Sandbox Code Playgroud)

问题是我的代码死了相当于id-1022:

opening handle to: /foo/bar/baz/id-0001/coordinates.bed
opening handle to: /foo/bar/baz/id-0002/coordinates.bed
...
opening handle to: /foo/bar/baz/id-1022/coordinates.bed
could not open handle to /foo/bar/baz/id-1022/coordinates.bed
0
6144 at ./process.pl line 66.
Run Code Online (Sandbox Code Playgroud)

Perl的上限是否可以打开或存储在哈希引用中的文件句柄数?或者我在别处犯了另一个错误?

Viv*_*sse 7

所有编程语言中每个进程的打开文件数量都有限制.

这实际上是操作系统强加的限制,以防止恶意(或虚假)程序消耗系统的所有资源,这可能导致操作系统冻结.

如果您使用的是基于Linux(非Mac)的操作系统,请查看ulimit/etc/security/limits.conf.

ulimit -n 2048
Run Code Online (Sandbox Code Playgroud)

这适用于大多数Linux发行版.

我不知道Mac的配置(它在这个特定点上与Unix不同)和/或Windows.


编辑:

os OS X的限制是使用该launchctl工具定义的:

launchctl limit maxfiles 2048 unlimited
Run Code Online (Sandbox Code Playgroud)


cdh*_*wie 6

存在操作系统强加的限制.请注意,stdin/stdout/stderr都计为FD.Linux上的默认FD限制为每个进程1024个. 这个问题提供了更多细节.

请注意,我使用的大多数Linux上的硬限制是1024.检查/etc/security/limits.conf(路径可能取决于您的发行版),看看是否可以增加它.

您可能还会考虑重写脚本,以便不需要立即打开所有这些文件.加载所有数据,或提供延迟加载机制,以便在需要时加载数据,然后关闭文件.