TAGS :Viewed: 2 - Published at: a few seconds ago

[ Getting multiprocessing Errors ]

So I have this;

from multiprocessing import Process

def run():
    4/0
    sys.exit()

def go(self):
    p = Process(target=run, args=())
    p.start()
    p.join()

How can get the errors from the Process and maybe store them in a file?

Answer 1


you can use log module: import logging and let each process log the errors/log directly in the log file.

logger = logging.getLogger('spam_application')
logger.warning("Something bad happened")

Do the following in your code. Note - This is a rotating logger you can use others too.[http://docs.python.org/2/library/logging.html]

from multiprocessing import Process
import logging
from logging.handlers import RotatingFileHandler 

r_logger = logging.getLogger('parsexmlfiles')

def set_logger()
    FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
    parser_logger = logging.getLogger('A_A_logfile')

    if isdaemon is True:
        # Log into a Log File.
        rotatingFH = RotatingFileHandler("/tmp/A_Alogfile.log", mode='a', 
                                     maxBytes=7340032, backupCount=4,
                                     encoding=None, delay=False)
        rotatingFH.setFormatter(logging.Formatter(
                                fmt="%(asctime)s : %(levelname)s : %(message)s", 
                                datefmt=None))
        parser_logger.addHandler(rotatingFH)
        parser_logger.setLevel(logging.DEBUG)

def run():
    4/0
    r_logger.info("Info Message")
    sys.exit()

def go(self):
    set_logger()
    p = Process(target=run, args=())
    p.start()
    p.join()

Answer 2


Wrap the function in try/except, blanket catch the exceptions. Use the python logging module to log the stacktrace, and perhaps locals() as well for context.

You could also skip the logging module and just use 'print' to print the exception handling messages to the console.