I have a main.py that execute file.py using subprocess.Popen:
# Execute request
url = "http://something:50001/dosomething/run"
test = ["python", "file.py", "url", url]
writer = subprocess.Popen(test, shell=False)
File.py execute a function that get an "exit code" when it request the url :
exitCode = None
answer = requests.get(url)
janswer = answer.json()
exitCode = janswer.get("exitCode")
return exitCode
For some reason, I need to execute this file.py using subprocess.Popen so that some code in main.py execute while file.py does its stuff.
What I'm trying to do now is to recover the exitCode from file.py to main.py.
I tried to use stdout=subprocess.Pipe and recover the exit code with a print, but for some reason it doesn't work.
Getting the writer.returncode isn't the solution to because it will give me the code from the subprocess, not from the function
function return exitCode = -2, subprocess return returncode = 0, I need that -2. (-2 is just an example, the exitCode isn't the same all the time and depends on some parameters.)
sys.exit()? This allows you to specify the exit code of the subprocess, which will then be reflected inwriter.returncode. Note that the range of values you can return withsys.exit()is limited but maybe it is sufficient to use only a few small values.stdout = PIPEdoes not work? It works fine for me.stdout = PIPE, for some obscure reason, send me back theurl, not theexitCode, there is noprint(url), just aprint(exitCode)and thereturn exitCode