Python OSError: [Errno 24] Too many open files

3

我在Mac OS 10.11.2上使用Ipython Notebook。我运行的是Python 3.5.1和Matplotlib 1.5.1,以及Seaborn版本0.6.0:

%matplotlib inline

import matplotlib.pyplot as plt

import seaborn as sns

我最近在使用Matplotlib/Seaborn绘图时遇到了问题。在同一个笔记本中生成几个图之后,最新的图所在的单元格崩溃并显示以下错误:

OSError: [Errno 24] Too many open files: '/Library/Fonts/Arial.ttf'

我仍然能够运行其他单元格,但每次尝试在此笔记本中绘制图形时都会出现相同的错误,直到我重新启动它。

我尝试通过运行以下命令来增加文件限制:

sudo launchctl limit maxfiles 10000000 10000000

似乎有一些帮助,但问题仍然存在。如果有任何解决该问题的建议,将不胜感激。谢谢!
以下是错误的精确输出:
OSError                                   Traceback (most recent call last)
/Users/spfraib/anaconda/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)

/Users/spfraib/anaconda/lib/python3.5/site-packages/IPython/core/pylabtools.py in <lambda>(fig)

/Users/spfraib/anaconda/lib/python3.5/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in draw(self)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/figure.py in draw(self, renderer)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/axis.py in draw(self, renderer, *args, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/axis.py in _get_tick_bboxes(self, ticks, renderer)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/text.py in get_window_extent(self, renderer, dpi)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/text.py in _get_layout(self, renderer)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/mathtext.py in parse(self, s, dpi, prop)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/mathtext.py in __init__(self, *args, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/mathtext.py in __init__(self, *args, **kwargs)

/Users/spfraib/anaconda/lib/python3.5/site-packages/matplotlib/mathtext.py in __init__(self, default_font_prop, mathtext_backend)

OSError: [Errno 24] Too many open files: '/Library/Fonts/Arial.ttf'
2个回答

2

在开始使用ipython之前,请尝试增加文件限制,例如ulimit -n 4096,最多可达到4096个文件描述符。El Capitan的默认文件描述符限制为256个。


重启ipython后,我尝试了:ulimit -n 4096,ulimit -n 100000。但问题仍然存在。 - spfraib

1
在这篇博客post中描述的解决方案为我解决了问题。我在使用Sublime Text编辑器时遇到了这些错误,该编辑器使用一个由Python驱动的插件系统。
创建/Library/LaunchDaemons/limit.maxfiles.plist文件,并添加以下内容:
<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"  
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">  
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>524288</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>  

创建 /Library/LaunchDaemons/limit.maxproc.plist 文件,并填入以下内容:
<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
<plist version="1.0">
  <dict>
    <key>Label</key>
      <string>limit.maxproc</string>
    <key>ProgramArguments</key>
      <array>
        <string>launchctl</string>
        <string>limit</string>
        <string>maxproc</string>
        <string>2048</string>
        <string>2048</string>
      </array>
    <key>RunAtLoad</key>
      <true />
    <key>ServiceIPC</key>
      <false />
  </dict>
</plist>

确保权限正确,

-rw-r--r--   1 root  wheel   541 Oct  5 14:14 limit.maxfiles.plist
-rw-r--r--   1 root  wheel   586 Oct  5 14:14 limit.maxproc.plist

然后:

sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
sudo launchctl load -w /Library/LaunchDaemons/limit.maxproc.plist

起初,对我来说它并没有起作用(检查ulimit -a),但是在重启后,它就像魔法一样奏效了。不再有烦人的错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接