繁体   English   中英

在python中使用多处理进行日志记录

[英]Logging with Multiprocessing in python

我目前正在努力使一个简单的多处理日志工作:

我正在从以下答案中使用MultiProcessingLoghttps : //stackoverflow.com/a/894284/293195

我有一个简单的ConverterImage ,它应该能够吐出日志,该日志有效,但是异常和tracelog永远不会出现在日志中? 有人知道这是什么问题吗?

import os, traceback,logging
import multiprocessing, threading, logging, sys, traceback
from multiprocessing import Pool, Manager
from logging import FileHandler

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

        self._handler = FileHandler(name, mode)
        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)


class ConvertImage:
    def __init__(self, logger=None):
        self.logger = logger

    def __call__(self,f):
        self.process(f)

    def process(self,f):
        try:
            logging.info("Process %i" % os.getpid() )
            raise NameError("Stupid error")

        except Exception as e:
            logging.info("Exception: " + e.message)
            exc_buffer = io.StringIO()
            traceback.print_exc(file=exc_buffer)
            logging.info(exc_buffer.getvalue())
            raise e
        except:
            logging.info("Exception!")
            exc_buffer = io.StringIO()
            traceback.print_exc(file=exc_buffer)
            logging.info(exc_buffer.getvalue())
            raise


mpl = MultiProcessingLog("ImageProcessing.log", mode='w+')
mpl.setFormatter( logging.Formatter('%(asctime)s - %(lineno)d - %(levelname)-8s - %(message)s') )
logger = logging.getLogger()
logger.addHandler(mpl)
logger.setLevel(logging.DEBUG)


pool = Pool();
converter = ConvertImage()
# map converter.process function over all files
result = pool.map_async(converter, ["A","B","C"]);

pool.close();
pool.join()
logging.shutdown()

在ConvertImage中,您使用日志记录模块函数而不是self.logger方法。 您可以尝试下面的代码吗?

def process(self,f):
    try:
        self.logger.info("Process %i" % os.getpid() )
        raise NameError("Stupid error")

    except Exception as e:
        self.logger.info("Exception: " + e.message)
        exc_buffer = io.StringIO()
        traceback.print_exc(file=exc_buffer)
        self.logger.info(exc_buffer.getvalue())
        raise e
    except:
        self.logger.info("Exception!")
        exc_buffer = io.StringIO()
        traceback.print_exc(file=exc_buffer)
        self.logger.info(exc_buffer.getvalue())
        raise

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM