Maintaining Logging and/or stdout/stderr in Python Daemon

后端 未结 3 2107
长情又很酷
长情又很酷 2020-12-14 19:02

Every recipe that I\'ve found for creating a daemon process in Python involves forking twice (for Unix) and then closing all open file descriptors. (See http://www.jejik.com

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 19:09

    We just had a similar issue, and due to some things beyond my control, the daemon stuff was separate from the stuff creating the logger. However, logger has a .handlers and .parent attributes that make it possible with something like:

        self.files_preserve = self.getLogFileHandles(self.data.logger)
    
    def getLogFileHandles(self,logger):
        """ Get a list of filehandle numbers from logger
            to be handed to DaemonContext.files_preserve
        """
        handles = []
        for handler in logger.handlers:
            handles.append(handler.stream.fileno())
        if logger.parent:
            handles += self.getLogFileHandles(logger.parent)
        return handles
    

提交回复
热议问题