Tornado [Errno 24] Too many open files [duplicate]

2024/9/7 19:48:35

We are running a Tornado 3.0 service on a RedHat OS and getting the following error:

[E 140102 17:07:37 ioloop:660] Exception in I/O handler for fd 11Traceback (most recent call last):File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 653, in startself._handlers[fd](fd, events)File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 241, in wrappedcallback(*args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/tornado/netutil.py", line 136, in accept_handlerconnection, address = sock.accept()File "/usr/lib/python2.7/socket.py", line 202, in accepterror: [Errno 24] Too many open files

But we couldn't figure out what that means.

Our Tornado code is as follows:

import sys
from tornado.ioloop import IOLoop
from tornado.options import parse_command_line, define, options
from tornado.httpserver import HTTPServer
from tornado.netutil import bind_sockets
import tornado
sys.path.append("..")from tornado.web import  RequestHandler, Application
from shared.bootstrap import *
from time import time
from clients import ClientFactoryfrom shared.configuration   import Config
from shared.logger          import Loggerfrom algorithms.neighborhood.application import NeighborhoodApplication
import tracebackdefine('port', default=8000, help="Run on the given port", type=int)
define('debug', default=True, help="Run application in debug mode", type=bool)class WService(RequestHandler):_clients = {}def prepare(self):self._start_time = time()RequestHandler.prepare(self)def get(self, algorithm = None):self.add_header('Content-type', 'application/json')response = {'skus' : []}algorithm = 'neighborhood' if not algorithm else algorithmtry:if not algorithm in self._clients:self._clients[algorithm] = ClientFactory.get_instance(algorithm)arguments = self.get_arguments_by_client(self._clients[algorithm].get_expected_arguments())response['skus'] = app.get_manager().make_recommendations(arguments)self.write(response)except Exception as err:self.write(response)error("Erro: " + str(err))def get_arguments_by_client(self, expected_arguments):arguments = {}for key in expected_arguments:arguments[key] = self.get_argument(key, expected_arguments[key])return argumentsdef on_connection_close(self):self.finish({'skus':[]})RequestHandler.on_connection_close(self)def on_finish(self):response_time = 1000.0 *(time() - self._start_time)log("%d %s %.2fms" % (self.get_status(), self._request_summary(), response_time))RequestHandler.on_finish(self)def handling_exception(signal, frame):error('IOLoop blocked for %s seconds in\n%s\n\n' % ( io_loop._blocking_signal_threshold, ''.join(traceback.format_stack(frame)[-3:])))if __name__ == "__main__":configuration = Config()Logger.configure(configuration.get_configs('logger'))app = NeighborhoodApplication({'application': configuration.get_configs('neighborhood'),'couchbase':   configuration.get_configs('couchbase'),'stock':       configuration.get_configs('stock')})app.run()log("Neighborhood Matrices successfully created...")log("Initiating Tornado Service...")parse_command_line()application = Application([(r'/(favicon.ico)', tornado.web.StaticFileHandler, {"path": "./images/"}),(r"/(.*)", WService)], **{'debug':options.debug, 'x-headers' : True})sockets = bind_sockets(options.port, backlog=1024)server = HTTPServer(application)server.add_sockets(sockets)io_loop = IOLoop.instance()io_loop.set_blocking_signal_threshold(.05, handling_exception)io_loop.start()

It's a very basic script, basically it gets the URL, process it in the make_recommendation function and sends back the response.

We've tried to set a tornado timeout of 50 ms through the io_loop.set_blocking_signal_threshold function as sometimes the processing of the URL might take this long.

The system receives around 8000 requests per minute and it worked fine for about 30 minutes, but after that it started throwing the "too many files error" and broke down. On general the requests were taking about 20 ms to get processed but when the error started happening the time consumed increased to seconds, all of a sudden.

We tried to see how many connections the port 8000 had and it had several open connections all with the "ESTABLISHED" status.

Is there something wrong in our Tornado script? We believe our timeout function is not working properly, but for what we've researched so far everything seems to be ok.

If you need more info please let me know.

Thanks in advance,

Answer

Many linux distributions ship with very low limits (e.g. 250) for the number of open files per process. You can use "ulimit -n" to see the current value on your system (be sure to issue this command in the same environment that your tornado server runs as). To raise the limit you can use the ulimit command or modify /etc/security/limits.conf (try setting it to 50000).

Tornado's HTTP server does not (as of version 3.2) close connections that a web browser has left open, so idle connections may accumulate over time. This is one reason why it is recommended to use a proxy like nginx or haproxy in front of a Tornado server; these servers are more hardened against this and other potential DoS issues.

https://en.xdnf.cn/q/73000.html

Related Q&A

How to check if an RGB image contains only one color?

Im using Python and PIL.I have images in RGB and I would like to know those who contain only one color (say #FF0000 for example) or a few very close colors (#FF0000 and #FF0001).I was thinking about us…

python requests and cx_freeze

I am trying to freeze a python app that depends on requests, but I am getting the following error:Traceback (most recent call last):File "c:\Python33\lib\site-packages\requests\packages\urllib3\ut…

django changing a date field to integer field cant migrate

I recently changed a date field to an integer field (the data was specified in number of months remaining rather than a date). However all though the make migrations command works fine when I attempt t…

Sqlalchemy get row in timeslot

I have a model called Appointment which has the columns datetime which is a DateTime field and duration which is an Integer field and represents duration in minutes. Now I want to check if func.now() i…

How do I include non-.py files in PyPI?

I am a newb to PyPI...so let me qualify with that. I am trying to put a package on PyPI but having a bit of trouble when I try to install it with pip. When I upload the file to PyPI, I get a warning (b…

How to create a custom AutoField primary_key entry within Django

I am trying to create a custom primary_key within my helpdesk/models.py that I will use to track our help desk tickets. I am in the process of writing a small ticking system for our office.Maybe there …

Multiple HoverTools for different lines (bokeh)

I have more than one line on a bokeh plot, and I want the HoverTool to show the value for each line, but using the method from a previous stackoverflow answer isnt working:https://stackoverflow.com/a/2…

Cant Install PIL 1.7

I have python 2.7.3 and I want to install PIL 1.7. I downloaded "PIL-1.1.7.win32-py2.7" and try to install it but it shows me an error messege that it cant find python 2.7 in the registry.&qu…

slice/split a layer in keras as in caffe

I have used this converter to convert a Caffe model to Keras. But one of my layers is of type slice and it needs to be converted as well but the converter currently does not support this and raises an …

Can I use regexes within datetime.strptime formats?

I have string values that contain a trailing time stamp. I thought I could use strptime with a regex pattern to extract those. Like:from __future__ import print_functionfrom datetime import datetime # …