Search code examples
pythonsubprocesspopen

subprocess.Popen, get variable from subprocess (child)


I would like to know how i can handle it, that i get a variable/value from the subprocess to the parent.

I am running the subprocess as an script. The parent looks like:

import subprocess
p = subprocess.Popen('abaqus python getData.py', shell=True)
p_status = p.wait()
print b

The child looks like:

from numpy import *

if __name__ == "__main__":

    b = [0,1,2,3]  # output is a list of integers
    global a = b

I am totally new to python. I think the problem is, that i can not store variables this way and make them "public" to the parent? Do i have to write them in a *.txt or something like that and get them with numpy.loadtxt()?


Solution

  • Even if the subprocess is a python process, you cannot communicate between them using global variables.

    You cannot use a multiprocessing.Manager object either since you need 2 distinct python engines (with separate capabilities).

    The classic (and easy) way is to serialize your data using a simple print (since you have a list of integers in output), and deserialize using ast.literal_eval (json would also be possible)

    Callee (getData.py mockup):

    if __name__ == "__main__":
        print([1,2,3,4])  # mock output
    

    Caller:

    import subprocess,ast
    output = subprocess.check_output('abaqus python getData.py', shell=True)
    lst = ast.literal_eval(output.decode("ascii"))
    

    now lst contains [1,2,3,4]

    If your output data is more complex (list of lists, dictionaries...) just compose the list of lists, dictionary with nested dicts inside, ... and print that. ast.literal_eval is able to rebuild the same structure on the other side.