Reraise exception from subprocess

I have code running in a subprocess that should throw an exception.

I would like to make the same exception in the main process when the exception is returned from the subprocess (preferably keeping the stack trace), but I'm not sure how to do it.

I take the stderr from the subprocess just fine, but I cannot find how to parse it, so I get an exception type. How would I do that?

I am using python 2.7

Main method

import subprocess example=subprocess.Popen(["python","example.py"], stdout = subprocess.PIPE, stderr = subprocess.PIPE) return_obj, return_error = example.communicate() if return_error: # replace with error from subprocess print "Should raise ",NameError('HiThere') raise TypeError('Wrong type') 

subprocess

 raise NameError('HiThere') 
+5
source share
2 answers

If all you need is your Python code running in a separate process, you probably shouldn't use subprocess . As Serge Ballesta said, the goal of subprocess is to run another program at the OS level without really worrying about what it is - you have nothing to help you deal with the intricacies of the python interpreter process.

For this purpose, it is best to simply import your code and use multiprocessing , which provides a high-level interface that will help you run python code in multiple processes.

Assuming you have a specific main function on example.py :

 from examply import main as example_main import multiprocessing pool= multiprocessing.Pool(1) pool.apply( example_main ) 

Using this code, both exceptions and return values ​​will be transparently passed to your main process.

You can also use Pool.apply_async if you do not want to block waiting for the result.

+3
source

The subprocess runs another native application. It can be java, C ++, lisp, or even Fortran or Cobol. Thus, you have only 2 ways to get an exception from a Python subprocess:

  • generally forget the subprocess and directly call python code in the same python program, with a simple try except will do the job
  • define in the interface a contract between the main and the subprocess, for example, requiring that the error output be empty if there is no fatal exeption, and it also contains an exception picker and stacktrace.
+1
source

Source: https://habr.com/ru/post/1203928/


All Articles