From 61aaed88d17a560323ad06e438fd1e0cde9c6b8e Mon Sep 17 00:00:00 2001 From: ruyer Date: Mon, 15 Jun 2026 11:28:56 +0200 Subject: [PATCH] add an helper to increase opened file soft limit - create a new module in silx.utils - call the new function in silx.app.view.main --- src/silx/app/view/main.py | 18 +++---------- src/silx/utils/system_limit.py | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 src/silx/utils/system_limit.py diff --git a/src/silx/app/view/main.py b/src/silx/app/view/main.py index 61ac5efe36..93b39809cc 100644 --- a/src/silx/app/view/main.py +++ b/src/silx/app/view/main.py @@ -34,6 +34,7 @@ import traceback from silx.app.utils import parseutils +from silx.utils.system_limit import increase_max_opened_files _logger = logging.getLogger(__name__) """Module logger""" @@ -102,20 +103,7 @@ def mainQt(options): # Import most of the things here to be sure to use the right logging level # - # Use max opened files hard limit as soft limit - try: - import resource - except ImportError: - _logger.debug("No resource module available") - else: - if hasattr(resource, "RLIMIT_NOFILE"): - try: - hard_nofile = resource.getrlimit(resource.RLIMIT_NOFILE)[1] - resource.setrlimit(resource.RLIMIT_NOFILE, (hard_nofile, hard_nofile)) - except (ValueError, OSError): - _logger.warning("Failed to retrieve and set the max opened files limit") - else: - _logger.debug("Set max opened files to %d", hard_nofile) + increase_max_opened_files() # This needs to be done prior to load HDF5 hdf5_file_locking = "TRUE" if options.hdf5_file_locking else "FALSE" @@ -131,10 +119,10 @@ def mainQt(options): ) import silx - from silx.gui import qt # Make sure matplotlib is configured import silx.gui.utils.matplotlib # noqa + from silx.gui import qt app = qt.QApplication.instance() if app is None: diff --git a/src/silx/utils/system_limit.py b/src/silx/utils/system_limit.py new file mode 100644 index 0000000000..8a6d2e19d0 --- /dev/null +++ b/src/silx/utils/system_limit.py @@ -0,0 +1,49 @@ +# /*########################################################################## +# +# Copyright (c) 2019 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ###########################################################################*/ +"""Utils function relative to system limit""" + +__authors__ = ["M. Ruyer"] +__license__ = "MIT" +__date__ = "05/06/2026" + +import logging + +_logger = logging.getLogger() + + +def increase_max_opened_files(): + """Use max opened files hard limit as soft limit""" + try: + import resource + except ImportError: + _logger.debug("No resource module available") + else: + if hasattr(resource, "RLIMIT_NOFILE"): + try: + hard_nofile = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + resource.setrlimit(resource.RLIMIT_NOFILE, (hard_nofile, hard_nofile)) + except (ValueError, OSError): + _logger.warning("Failed to retrieve and set the max opened files limit") + else: + _logger.debug("Set max opened files to %d", hard_nofile)