python - 使用多处理记录失败

问题描述 投票:0回答:1

我正在尝试使用多处理为我们的应用程序(烧瓶)实现日志记录。我们使用python2.7,我使用队列的概念来保​​留队列中存在的所有分支和日志记录的日志请求。我跟着这个approach。只是从该链接更改是我使用TimedRotatatingFileHandler而不是RotatingFileHandler。这是我的dictconfig

我在初始化分支之前初始化记录器,并在代码中按以下方式初始化

    from flask import Flask
    from tornado.wsgi import WSGIContainer 
    from tornado.httpserver import HTTPServer
    from tornado.ioloop import IOLoop


    path = 'share/log_test/logging.yaml'
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = yaml.load(f.read())
        logging.config.dictConfig(config)

    logger = logging.getLogger('debuglog') # problem starts if i keep this statement

    app = Flask(__name__)
    init_routes(app) # initialize our routes
    server_conf = config_manager.load_config(key='server')
    logger.info("Logging is set up.") # only this line gets logged and other log statement to be logged by forks in code with same logger are not writing to the file.

    http_server = HTTPServer(WSGIContainer(app))

    http_server.bind(server_conf.get("PORT")) # port to listen
    http_server.start(server_conf.get("FORKS")) # number of forks
    IOLoop.current().start()

我面临的问题是,如果我在初始化分支之前在代码中使用getLogger,则分支不会将日志写入日志文件,只会记录初始化分支之前的日志语句。如果我删除logging.getLogger('debuglog'),叉子正确记录。

我暂停了执行流程并验证了处理程序是否已分配给记录器,但似乎没问题

为什么会出现这种奇怪的行为?

更新:当我使用另一个具有相同文件的记录器进行编写时,一切正常。但是,当我使用相同的记录器时,它不起作用。任何与RLock有关的事情?

python python-2.7 logging multiprocessing
1个回答
0
投票

我终于得到了解决方案。我在实现中删除了队列的概念,然后在收到日志记录后立即打印。

    def emit(self, record):
        try:
            s = self._format_record(record)
            self._handler.emit(record) #emitting here itself
            # self.send(s) #stopped sending it to queue 
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)

这似乎与以下测试工作正常

8 workers - 200 requests - 50 concurrency

© www.soinside.com 2019 - 2024. All rights reserved.