Retrieve the Outputs from subprocess.Popen

Hi, everyone.

I have read that there are some discussions about using the subprocess.Popen() or subprocess.run() module to run CPython code. I have managed to call functions written in CPython by subprocess modules to make the plots. However, I have trouble retrieving the outputs from Cpython. Normally, subprocess.Popen() can use the pipeline to return outputs and error codes. But this is not working with ANSYS IronPython. Can someone shed some light on reading outputs from Cpython functions? I am using the latest ANSYS 2024R2.

Best Answer

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 125
    100 Comments Second Anniversary 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭
    Answer ✓

    Hi @Gordon1998 ,

    @Gordon1998

    I do not think you have shared with me the working script. The shared script will not work in CPython and IronPython.

    But here is what I am trying, both in CPython and IronPython.

    I have two scripts:

    1. add_times.py having below content:
    import sys
    def add_times(a, b):
        c = a + b
        d = a * b
        return c, d
    
    if __name__=='__main__':
        print(add_times(float(sys.argv[1]),float(sys.argv[2])))
    
    1. subprocess_test.py
    import subprocess
    
    a = 1
    b = 2
    process = subprocess.Popen([r"D:\Workshop\demo\demo_crng_env\Scripts\python.exe","D:/add_times.py", 
                     str(a), str(b)],
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.PIPE)
    
    
    stdout, stderr = process.stdout.read(), process.stderr.read()
    
    print("Output:", stdout)
    print("Error:", stderr if stderr else "No error")
    

    Below are the outputs:

    From IronPython in mechanical:

    From CPython in VSCode:

Answers

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 125
    100 Comments Second Anniversary 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭
    edited December 2024

    Does this help?

    `
    import subprocess

    Define the command you want to run

    command = [r"C:/Users/56577474/.ansys_python_venvs/pygeom_2511/Scripts/python.exe", "-m","pip","list"]

    Start the subprocess and capture the output

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    Read the output and error

    stdout, stderr = process.stdout.read(), process.stderr.read()

    print("Output:", stdout)
    print("Error:", stderr if stderr else "No error")

    `

  • Gordon1998
    Gordon1998 Member Posts: 12
    Name Dropper First Comment
    **

    @Rajesh Meena said:
    Does this help?

    `
    import subprocess

    Define the command you want to run

    command = [r"C:/Users/56577474/.ansys_python_venvs/pygeom_2511/Scripts/python.exe", "-m","pip","list"]

    Start the subprocess and capture the output

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    Read the output and error

    stdout, stderr = process.stdout.read(), process.stderr.read()

    print("Output:", stdout)
    print("Error:", stderr if stderr else "No error")

    `

    Hi, Rajesh. Thanks for your reply. I have tried this method before. However, the IronPython adopted by ANSYS ACT is quite old. It does not have a subprocess.PIPE feature. If you run the code, you will realize that stdout reads nothing from the subprocess. But if you run this in Cpython, this code will work.

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 125
    100 Comments Second Anniversary 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭

    @Gordon1998 I tried this in Ansys 2024R2 mechanical scripting console and it worked fine. I got a list of modules installed in that particular python environment via subprocess.

    How are you running IronPython script?

  • Gordon1998
    Gordon1998 Member Posts: 12
    Name Dropper First Comment
    **

    Hi Rajesh, thanks for your reply. I have tried your scripts before. Let's say we have a simple function that is stored in a separate file called 'add_times.py' as shown below:

    def add_times(a, b):
        c = a + b
        d = a * b
        return c, d
    

    I call this function from the 'main.py' as:

    a = 1
    b = 2
    subprocess.Popen("python","add_times.py", 
                     str(a), str(b),
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.PIPE)
    

    If I deploy this code into ANSYS ACT, both 'stdout' and 'stderr' will return 'None'. However, if I deploy the same code in VSCode (I use Cpython), this code can return the outputs. So I am afraid that 'subprocess.PIPE' is not implemented in ANSYS ACT (IronPython).

  • Gordon1998
    Gordon1998 Member Posts: 12
    Name Dropper First Comment
    **

    Hi, Rajesh. This works on my computer! Thank you so much.

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 125
    100 Comments Second Anniversary 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭

    @Gordon1998

    Great! I have selected my last response as the answer to this post.