diff --git a/notebook/notebookapp.py b/notebook/notebookapp.py index d88a1bdb5f..9c90fb677f 100755 --- a/notebook/notebookapp.py +++ b/notebook/notebookapp.py @@ -32,8 +32,13 @@ import warnings import webbrowser -from base64 import encodebytes +try: + import resource +except ImportError: + # Windows + resource = None +from base64 import encodebytes from jinja2 import Environment, FileSystemLoader @@ -794,6 +799,14 @@ def _token_default(self): """ ) + min_open_files_limit = Integer(4096, config=True, + help=""" + Gets or sets a lower bound on the open file handles process resource + limit. This may need to be increased if you run into an + OSError: [Errno 24] Too many open files. + This is not applicable when running on Windows. + """) + @observe('token') def _token_changed(self, change): self._token_generated = False @@ -1371,6 +1384,23 @@ def init_logging(self): logger.parent = self.log logger.setLevel(self.log.level) + def init_resources(self): + """initialize system resources""" + if resource is None: + self.log.debug('Ignoring min_open_files_limit because the limit cannot be adjusted (for example, on Windows)') + return + + old_soft, old_hard = resource.getrlimit(resource.RLIMIT_NOFILE) + soft = self.min_open_files_limit + hard = old_hard + if old_soft < soft: + if hard < soft: + hard = soft + self.log.debug( + 'Raising open file limit: soft {}->{}; hard {}->{}'.format(old_soft, soft, old_hard, hard) + ) + resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard)) + def init_webapp(self): """initialize tornado webapp and httpserver""" self.tornado_settings['allow_origin'] = self.allow_origin @@ -1665,6 +1695,7 @@ def initialize(self, argv=None): self.init_logging() if self._dispatching: return + self.init_resources() self.init_configurables() self.init_server_extension_config() self.init_components()