do not always import matplotlib
See original GitHub issuelibrosa always imports matplotlib
, even if you are doing pure analysis and have no interest in graphing.
This is inconvenient for batch soundfile analysis, as matplotlib has a large memory footprint, and an even larger file handle footprint. Moreover, if you run matplotlib in certain environments (I’m using tornado to analyse soundfiles on request) it seems to leak file handles.
Whilst the file handle leak is not, AFAICT, librosa’s problem per se, I’ve run into this issue in enough circumstances to feel that matplotlib should be considered something to be imported only when strictly required, and the memory problem alone is annoying when processing thousands of files in a multiprocess batch.
At first glance it seems that avoiding this could be as simple as only importing librosa.display
on demand as opposed to in the root __init__.py
.
Would it be worth me filing a pull request to do that?
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
FYI: Instead of using the above hack this solved a number of librosa/matplotlib import issues for me
Yes, I haven’t mentioned the details here because it’s one of those irritating things that lies at the intersection of several projects. But for other weary travellers, there the issue is that gc does not always close files at the best of times, and that the quirky matplotlib backend opens lots of files for reasons to do with fonts. The problem seems to arise especially often if you are using something that does live code reloading, such as a web development server.
There are a couple of long-delayed matplotlib backend changes which will radically change the font handling when they land, although they are many months in review already: https://github.com/matplotlib/matplotlib/pull/5414 https://github.com/matplotlib/matplotlib/pull/4143
But in the mean time @TimSchmeier 's tip actually does ease the issue even though without the MPL backend; the Agg Matplotlib backend can run for a lot more code iterations (at least on my python) before running out of file handles. I don’t know why.