簡體   English   中英

python - 使用多處理記錄失敗

[英]python - Logging failure with multiprocessing

我正在嘗試使用多處理為我們的應用程序(燒瓶)實現日志記錄。 我們使用python2.7,我使用隊列的概念來保​​留隊列中存在的所有分支和日志記錄的日志請求。 我遵循這種方法 只是從該鏈接更改是我使用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') ,則forks正確記錄。

我暫停了執行流程並驗證了處理程序是否已分配給記錄器,但似乎沒問題

為什么會出現這種奇怪的行為?

更新:當我使用另一個具有相同文件的記錄器進行編寫時,一切正常。 但是,當我使用相同的記錄器時,它不起作用。 任何與RLock有關的事情?

我終於得到了解決方案。 我在實現中刪除了隊列的概念,然后在收到日志記錄后立即打印。

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM