更改Erlang文件句柄限制?
我遇到了一个Erlang OTP+牛仔应用程序的问题,它不允许我同时打开足够多的文件 如何更改梁中允许的打开文件句柄数 我可能需要同时打开大约500个小文本文件,但文件限制似乎是224。我从这个小测试程序中得到了224的值:更改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
-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}