python - 在 Python 中使用多处理时我应该如何记录?

现在我在一个框架中有一个中心模块,它使用 Python 2.6 multiprocessing module 生成多个进程。 .因为它使用multiprocessing,所以有模块级的multiprocessing-aware log,LOG = multiprocessing.get_logger()。根据 the docs ,这个记录器(EDIT)确实没有有进程共享锁,这样你就不会在sys.stderr(或其他文件句柄)通过同时写入多个进程来实现。

我现在遇到的问题是框架中的其他模块不支持多处理。在我看来,我需要让这个中央模块的所有依赖项都使用多处理感知日志记录。这很烦人在框架内,更不用说框架的所有客户端了。有没有我没有想到的替代方案?

最佳答案

我刚刚编写了一个自己的日志处理程序,它只是通过管道将所有内容提供给父进程。我只测试了十分钟,但它似乎工作得很好。

(注意:这是硬编码为RotatingFileHandler,这是我自己的用例。)


更新:@javier 现在将这种方法作为 Pypi 上可用的包进行维护 - 请参阅 multiprocessing-logging在 Pypi 上,github 上 https://github.com/jruere/multiprocessing-logging


更新:实现!

这现在使用队列来正确处理并发,并且还可以正确地从错误中恢复。我现在已经在生产中使用它几个月了,下面的当前版本可以正常工作。

from logging.handlers import RotatingFileHandler
import multiprocessing, threading, logging, sys, traceback

class MultiProcessingLog(logging.Handler):
    def __init__(self, name, mode, maxsize, rotate):
        logging.Handler.__init__(self)

        self._handler = RotatingFileHandler(name, mode, maxsize, rotate)
        self.queue = multiprocessing.Queue(-1)

        t = threading.Thread(target=self.receive)
        t.daemon = True
        t.start()

    def setFormatter(self, fmt):
        logging.Handler.setFormatter(self, fmt)
        self._handler.setFormatter(fmt)

    def receive(self):
        while True:
            try:
                record = self.queue.get()
                self._handler.emit(record)
            except (KeyboardInterrupt, SystemExit):
                raise
            except EOFError:
                break
            except:
                traceback.print_exc(file=sys.stderr)

    def send(self, s):
        self.queue.put_nowait(s)

    def _format_record(self, record):
        # ensure that exc_info and args
        # have been stringified.  Removes any chance of
        # unpickleable things inside and possibly reduces
        # message size sent over the pipe
        if record.args:
            record.msg = record.msg % record.args
            record.args = None
        if record.exc_info:
            dummy = self.format(record)
            record.exc_info = None

        return record

    def emit(self, record):
        try:
            s = self._format_record(record)
            self.send(s)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)

    def close(self):
        self._handler.close()
        logging.Handler.close(self)

https://stackoverflow.com/questions/641420/

相关文章:

python - 如何在 Django 中按日期范围过滤查询对象?

python - 如何在给定的图上绘制垂直线

linux - 如何限制递归文件列表的深度?

linux - 在一行中执行组合多个 Linux 命令

linux - 如何在 Linux 上通过 FTP 递归下载文件夹

linux - 如何使用 YUM 列出包的内容?

python - 配置以便 pip install 可以从 github 工作

linux - 如何在 Linux 上按名称而不是 PID 杀死进程?

windows - 如何将 DOS/Windows 换行符 (CRLF) 转换为 Unix 换行符

python - 如何在 Python 中生成动态(参数化)单元测试?