2

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.)

Hans
  • 2,419
  • 2
  • 30
  • 37
benvdg
  • 45
  • 7
  • Would it be an option to terminate the subprocess using `sys.exit()`? This allows you to specify the exit code of the subprocess, which will then be reflected in `writer.returncode`. Note that the range of values you can return with `sys.exit()` is limited but maybe it is sufficient to use only a few small values. – Daniel Junglas Sep 15 '20 at 10:49
  • that could be usefull for something else, but not right now. I get a code from the url execution, the code is dynamic @DanielJunglas No but thanks for helping buran – benvdg Sep 15 '20 at 10:52
  • Could you be more explicit why using `stdout = PIPE` does not work? It works fine for me. – Daniel Junglas Sep 15 '20 at 10:57
  • ```stdout = PIPE```, for some obscure reason, send me back the ```url```, not the ```exitCode```, there is no ```print(url)```, just a ```print(exitCode)``` and the ```return exitCode``` – benvdg Sep 15 '20 at 11:02
  • i think the reason why you are not getting the return value is because the subprogram isnt over yet when you try to get the return value. I suggest to use asyc await concept here – Sandrin Joy Sep 15 '20 at 11:04
  • The subprogram is over when I try to get the return value, I double checked it just to be sure, I will check asyc await concept to, more knowledge can't do no harm ! – benvdg Sep 15 '20 at 11:07
  • If you get unexpected output on `stdout` then I think you should debug where this comes from. It does not mean that `PIPE` does not work. It only means that you subprocess outputs more than it should. If you cannot avoid additional output on `stdout` then you could also try to use `stderr` instead. – Daniel Junglas Sep 15 '20 at 11:09

1 Answers1

0

Here is a short program that illustrates how you can get a number from a subprocess either via pipe or sys.exit(). Note that the range of values that can be returned from sys.exit() is limited, though.

import sys
import subprocess

def sub():
    try:
        print(42)
    except:
        # Print something even in case of an exception                          
        print(-1)
        raise
    sys.exit(43)

def main():
    writer = subprocess.Popen(["python3", sys.argv[0], "sub"],
                              stdout = subprocess.PIPE)
    for line in writer.stdout:
        print(int(line))
    exitcode = writer.wait()
    print('exitcode', exitcode)

if len(sys.argv) > 1 and sys.argv[1] == 'sub':
    sub()
else:
    main()

This prints

42
exitcode 43
Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22