我可以在Perl中打开的文件句柄数量是否有限制? [英] Is there a limit to the number of file handles I can open in Perl?

查看:155
本文介绍了我可以在Perl中打开的文件句柄数量是否有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am setting up a hash reference containing file handles.

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

The fourth column of my input file contains an identifier field that I am using to name the file handle's destination:

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

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

I use GNU core utilities to get a list of the identifiers:

$ cut -f4 myFile | sort | uniq
id-0001
id-0002
...

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

There can be more than 1024 unique identifiers in this column, and I need to open a file handle for each identifier and put that handle into a hash reference.

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};                                                                                                                                                                                           
    }                                                                                                                                                                                                                              
}       

问题是我的代码死了等价 id-1022

The problem is that my code is dying at the equivalent of 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.

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

Is there an upper limit in Perl to the number of file handles I can open or store in a hash reference? Or have I made another mistake elsewhere?

推荐答案

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

There is a limit to the number of open files per process in all programming languages.

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

This is actually a limit imposed by the operating system to prevent malicious (or bogus) programs to consume all the resources of the system, which could cause a freeze of the OS.

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

If you are using a Linux-based (non-Mac) OS, check out ulimit and /etc/security/limits.conf.

ulimit -n 2048

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

This should work on most Linux distros.

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

I don't know the configuration for Mac (it differs from Unix on this specific point) and/or Windows.

编辑:

限制操作系统OS X使用 launchctl 工具定义:

The limit os OS X is defined using the launchctl tool:

launchctl limit maxfiles 2048 unlimited

这篇关于我可以在Perl中打开的文件句柄数量是否有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆