Search code examples
pythonshelloutputabaqus

Python: Accessing the variables from a Subprocess (Child) script, in the parent script


Using Python 2.7, I have a main script which executes a subprocess script. My question is how do I capture certain variables from the subprocess, e.g. the vectors:

vec_1, vec_2, vec_3

I know that it is possible to obtain the stdout from the script as matrix of characters using subprocess.communicate(), however that doesn't suit my intended purpose. Here is what I have tried so far.

The Parent Script:

import subprocess
if __name__ == "__main__":
    process = subprocess.Popen(["abaqus", "python", "childscript.py", \
        "file"], shell=True, stdout=subprocess.PIPE)
    process.wait()
    pr = process.communicate()  

Child Subprocess:

def function(a):
    return vec_1, vec_2, vec_3

if __name__ == '__main__':
    function()

Bonus marks if anyone has an idea how to execute the subprocess without using shell=True. It seems to only be possible with the shell as it involves executing a .bat file.

EDIT

TO clarify why I am not directly importing the function into the main script:

The program executed in the subprocess, Abaqus has it's own python interface and it allows a python script to be used to access it's data outputs. Hence, the python script should be run through the program and cannot be integrated into the main.


Solution

  • For your interest, what I wanted to do is not possible. If it was simply a case of executing the child script along then Anentropic would be correct - the multiprocessing module is a better way to go.

    As my question was regarding a 3rd party program that has a python interface, unfortunately it is not possible.

    Reference: https://www.researchgate.net/post/How_can_I_access_the_ABAQUS_python_API_from_outside_ABAQUS_cae