Home > Enterprise >  How to input a file into a subprocess without writing to a drive?
How to input a file into a subprocess without writing to a drive?

Time:11-05

I am working on an optimizer for aircraft geometries and in the optimization process, it's needed to run a simulation in AVL (open source, Fortran), which takes the geometry as input and outputs useful data. Currently, the AVL part of the program works like this:

def resultados_avl(aircraft, command):
    process = subprocess.Popen(['avl'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

    path = create_file(aircraft, False)
    if command[0] == 'alpha':
        out = process.communicate(bytes('load %s\noper\na a %.3f\nx\nst\n\nquit\n' % (path, command[1]), 'utf-8'))[0]
    if command[0] == 'trim':
        out = process.communicate(bytes('load %s\noper\na pm %.3f\nx\nst\n\nquit\n' % (path, 0), 'utf-8'))[0]
        
    process.terminate()
    output = out.decode('utf-8')
    results = dict()
    # Proceeds to do some RegEx "fun" in AVL's output

The problem with this method is the need to create a file for AVL to read. It is a simple text file with a geometry description, but as you know, reading and writing files is slow and performance is quite important in an optimizer.

My question: is there a way to get the input to AVL without writing to a drive?

I have thought of modifying the Fortran source code to take the input buffer from stdin, but I don't even know where to start.

CodePudding user response:

As Vladimir F pointed out, this can be done by removing the open() and close() statements and changing the unit used for the read() statement.

In the AVL source code, the geometry input is processed in the ainput.f file. The changes I made were commenting lines 72 and 1096, where the open() and close() statements are, and changing the unit in line 1152 to *, as follows:

20 READ (*,1000,END=80,ERR=90) LINE

Now, instead of reading from the specified file, AVL reads from stdin.

  • Related