Tornado “error: [Errno 24] Too many open files” error

前端 未结 1 1019
暖寄归人
暖寄归人 2020-12-19 16:59

I\'ve worked with Tornado quite a bit, but this is the first time I\'ve run into this sort of error. I\'ve been working on a very basic URL shortener. URLs are put into the

相关标签:
1条回答
  • 2020-12-19 17:17

    Very simple, the RequestHandler object is instantiated for every request. Which means that the cached object that you're saving is on the RequestHandler (e.g. expand) object.

    If you were to add a simple "print 'CREATED!'" to the dbmongo(...) function you would see that it's created on every GET request.

    What you need to do is attach the handler to the class object, or a "global" as needed, though the best case is to put it on the Tornado Application object.

    Simple:

    class setup(tornado.web.RequestHandler):
        @classmethod
        def dbmongo(cls):
            if not hasattr(cls, '_dbmongo'):
                cls._dbmongo = apymongo.Connection("127.0.0.1", 27017)
            return cls._dbmongo
    

    Second approach is just to make it a global in your file:

    dbmongo_connection = None
    def dbmongo():
        if not dbmongo_connection:
            dbmongo_connection = apymongo.Connection("127.0.0.1", 27017)
        return dbmongo_connection
    

    Both of these have the same problem which is that if you have lots of classes that want to use the DB connection, it's harder to share it. Since the DB is a shared entity you probably want one for your whole application.

    class MongoMixin(object):
        def mongodb(self):
            if not hasattr(self.application, 'mongodb'):
                self.application.mongodb = apymongo.Connection(self.application.settings.get("mongohost", "127.0.0.1"), 27017)
            return self.application.mongodb
    
    class expand(tornado.web.RequestHandler, MongoMixin):
        def get(self):
           db = self.mongodb()
    
    0 讨论(0)
提交回复
热议问题