更改Erlang文件句柄限制?

问题描述 投票:3回答:1

我遇到了Erlang OTP + Cowboy应用程序的问题,该应用程序不允许我同时打开足够的文件。

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

潜在地,我需要同时打开大约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).

这给了

$ erl
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [kernel-poll:false]

Eshell V9.2  (abort with ^G)
1> c(test_fds).     
{ok,test_fds}
2> test_fds:count().
{emfile,224}
3> 

这似乎是一个Erlang问题,而不是Mac OSX问题,因为从命令行,我得到:

$ sysctl -h kern.maxfiles
kern.maxfiles: 49,152
$ sysctl -h kern.maxfilesperproc
kern.maxfilesperproc: 24,576
erlang file-handling beam
1个回答
5
投票

打开文件描述符的数量很可能受到shell的限制。您可以通过在调用ulimit -n 1000之前在shell中运行erl(或更多)来增加它。在我的系统上,默认值为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的恒定差异。

© www.soinside.com 2019 - 2024. All rights reserved.