更改Erlang文件句柄限制?

更改Erlang文件句柄限制?,erlang,file-handling,beam,Erlang,File Handling,Beam,我遇到了一个Erlang OTP+牛仔应用程序的问题,它不允许我同时打开足够多的文件 如何更改梁中允许的打开文件句柄数 我可能需要同时打开大约500个小文本文件,但文件限制似乎是224。我从这个小测试程序中得到了224的值: -module(test_fds). -export([count/0]). count() -> count(1, []). count(N, Fds) -> case file:open(integer_to_list(N), [write]) of

我遇到了一个Erlang OTP+牛仔应用程序的问题,它不允许我同时打开足够多的文件

如何更改梁中允许的打开文件句柄数

我可能需要同时打开大约500个小文本文件,但文件限制似乎是224。我从这个小测试程序中得到了224的值:

-module(test_fds).
-export([count/0]).

count() -> count(1, []).

count(N, Fds) ->
  case file:open(integer_to_list(N), [write]) of
    {ok, F} ->
      count(N+1, [F| Fds]);

    {error, Err} ->
      [ file:close(F) || F <- Fds ],
      delete(N-1),
      {Err, N}
  end.

delete(0) -> ok;
delete(N) -> 
  case file:delete(integer_to_list(N)) of
    ok         -> ok;
    {error, _} -> meh
  end,

delete(N-1).
这似乎是一个Erlang问题,而不是Mac OSX问题,因为从命令行中,我得到:

$ sysctl -h kern.maxfiles
kern.maxfiles: 49,152
$ sysctl -h kern.maxfilesperproc
kern.maxfilesperproc: 24,576

打开的文件描述符的数量很可能受到shell的限制。在调用
erl
之前,可以通过在shell中运行
ulimit-n1000
(或更多)来增加它。在我的系统上,默认值是7168,在返回
emfile
之前,脚本可以打开7135个文件。以下是我使用不同的
ulimit
值运行脚本的输出:

$ ulimit -n 64; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,32}
$ ulimit -n 128; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,96}
$ ulimit -n 256; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,224}
$ ulimit -n 512; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,480}

erl
最有可能在开始评估我们的代码之前打开32个文件描述符,这解释了
ulimit
和输出中32个常量的差异。

在您的shell中
ulimit-n
的输出是什么?在我的shell中
ulimit
返回
unlimited
,但这显然不是真的!谢谢这就解决了!然而,在我的机器上
ulimit
返回
unlimited
——这显然不是真的!
ulimit-n
打印什么?我的纯
ulimit
的输出也是
无限制的
-n
是打印打开文件描述符限制的方式。
ulimit-n
设置为256。谢谢
$ ulimit -n 64; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,32}
$ ulimit -n 128; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,96}
$ ulimit -n 256; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,224}
$ ulimit -n 512; erl -noshell -eval 'io:format("~p~n", [test_fds:count()]), erlang:halt()'
{emfile,480}