Note that there are some explanatory texts on larger screens.

plurals
  1. POCatching unpickleable exceptions and re-raising
    primarykey
    data
    text
    <p>This is a followup to my question <a href="https://stackoverflow.com/questions/8785899/hang-in-python-script-using-sqlalchemy-and-multiprocessing">Hang in Python script using SQLAlchemy and multiprocessing</a>. As discussed in that question, pickling exceptions is problematic in Python. This is usually not a issue, but one case when it is, is when errors occur in the python multiprocessing module. Since multiprocessing moves objects around by pickling, if an error occurs inside a multiprocessing process, the entire process may hang, as demonstrated in that question.</p> <p>One possible approach is to fix all the problematic exceptions, as discussed in that question. This is not easy, since one cannot easily know in advance which exceptions may be called. An alternative approach, which was <a href="https://stackoverflow.com/a/8786557/350713">suggested by lbolla in an answer to the question</a>, is to catch the exception, construct an equivalent harmless exception, and then rethrow. However, I'm not sure of exactly how to do this. Consider the following code.</p> <pre><code>class BadExc(Exception): def __init__(self, message, a): '''Non-optional param in the constructor.''' Exception.__init__(self, message) self.a = a import sys try: try: #print foo raise BadExc("bad exception error message", "a") except Exception, e: raise Exception(e.__class__.__name__ + ": " +str(e)), None, sys.exc_info()[2] except Exception, f: pass import cPickle a = cPickle.dumps(f) l = cPickle.loads(a) print "raising error" raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2] </code></pre> <p>This code pickles and unpickles the exception, and then throws it, giving the error</p> <pre><code>raising error Traceback (most recent call last): File "&lt;stdin&gt;", line 11, in &lt;module&gt; Exception: BadExc: bad exception error message </code></pre> <p>Credits to <a href="https://stackoverflow.com/a/1350981/350713">Glenn Maynard's answer to "“Inner exception” (with traceback) in Python?"</a>. This has the important stuff, namely the traceback, the error message, and the exception type, so this might be the best one can do. But ideally I'd like something that looks exactly like the original exception, namely</p> <pre><code>Traceback (most recent call last): File "&lt;stdin&gt;", line 11, in &lt;module&gt; __main__.BadExc: bad exception error message </code></pre> <p>or more generally, with the name of the exception in the front, rather than <code>Exception</code>. Is this possible?</p> <p>Alternatively, instead of the <code>BadExc</code> class, one can use the <code>print foo</code> statement instead, which gives a <code>NameError</code>. However, this exception does not require special handling. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload