Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

My, not so nice, solution is the following: The idea is that we catch the exception e in the forked process and just return it as the object e. Then, after calling this forked process, we check whether the returned value is in fact an exception. If so, we raise it. Of course this has several drawbacks (what if a function really wants to return an exception? also the traceback gets more complicated) but seems to work okay for me. If anyone has a nicer solution, I would be very interested to learn about it!

def encode_exceptions(func, *args, **kwargs) :
    try :
        ret = func(*args, **kwargs)
        return ret
    except Exception as e:
        return e

def decode_exceptions(ret) :
    if isinstance(ret, Exception) :
        raise ret
    else :
        return ret

def fork_func(func, *args, **kwargs) :
    @fork
    def forked_func():
        return encode_exceptions(func, *args, **kwargs)
    ret = forked_func()
    return decode_exceptions(ret) 

def test() :
    raise ValueError("Found a value error")

print(fork_func(test))