Logging module: too many open file descriptors

旧街凉风 提交于 2020-01-24 12:02:26

问题


I am using Python logging module to print logs to a file, but I encountered the issue that "too many open file descriptors", I did remember to close the log file handlers, but the issue was still there.

Below is my code

class LogService(object):
    __instance = None
    def __init__(self):
        self.__logger = logging.getLogger('ddd')
        self.__handler = logging.FileHandler('/var/log/ddd/ddd.log')
        self.__formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
        self.__handler.setFormatter(self.__formatter)
        #self.__logger.addHandler(self.__handler)

    @classmethod
    def getInstance(cls):
        if cls.__instance == None:
            cls.__instance = LogService()

        return cls.__instance

    # log Error
    def logError(self, msg):
        self.__logger.addHandler(self.__handler)
        self.__logger.setLevel(logging.ERROR)
        self.__logger.error(msg)
        # Remember to close the file handler
        self.closeHandler()

    # log Warning
    def logWarning(self, msg):
        self.__logger.addHandler(self.__handler)
        self.__logger.setLevel(logging.WARNING)
        self.__logger.warn(msg)
        # Remember to close the file handler
        self.closeHandler()

    # log Info
    def logInfo(self, msg):
        self.__logger.addHandler(self.__handler)
        self.__logger.setLevel(logging.INFO)
        self.__logger.info(msg)
        # Remember to close the file handler
        self.closeHandler()

    def closeHandler(self):
        self.__logger.removeHandler(self.__handler)
        self.__handler.close()

And after running this code for a while, the following showed that there were too many open file descriptors.

[root@my-centos ~]# lsof | grep ddd | wc -l
11555

回答1:


No no. The usage is far simpler

import logging
logging.basicConfig()

logger = logging.getLogger("mylogger")
logger.info("test")
logger.debug("test")

In your case you are appending the handler in every logging operation, which is at least overkill.

Check the documentation https://docs.python.org/2/library/logging.html




回答2:


Each time you log anything, you add another instance of the handler.

Yes, you close it every time. But this just means it takes slightly longer to blow up. Closing it doesn't remove it from the logger.

The first message, you have one handler, so you open one file descriptor and then close it.

The next message, you have two handlers, so you open two file descriptors and close them.

The next message, you open three file descriptors and close them.

And so on, until you're opening more file descriptors than you're allowed to, and you get an error.

To solution is just to not do that.



来源:https://stackoverflow.com/questions/30099038/logging-module-too-many-open-file-descriptors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!