Re raise the python exception is a different process than it is originated

Viewed 23

I have two separate processes client.py and service.py. I communicate between the two using json serializing format. Suppose service.py raises an exception and I collect the exception object like so:

service.py

import sys
import traceback

try:
    1/0
except ZeroDivisionError:
    ex_typ, ex, tb = sys.exc_info()
    exc_st = traceback.format_exception(ex_typ, ex, tb)
    exc_st = "\n".join(exc_st)

because I have to send the result over the wire I will serialze it like:

import json
res = {"result": [str(ex_typ), exc_st]}
json.dumps(res)

Now, I want to re-raise the same exception in the client.py module/process, currently I workaround is some thing like

import json

exc_typ, exc_st = json.loads(res)["result"]

I am not sure how can I convert exc_typ which is in str format to actual exception type(ZeroDivisionError) and raise with exc_st stacktrace (which should only holds service module traceback)?

Could someone share their thoughts?

Cheers, DD.

0 Answers
Related