Telesto Telesto - 4 days ago 6
Python Question

Close Octave-cli instances while using Oct2Py in loop

So I'm running into an increasingly annoying problem while using Oct2Py in my python code to run a few Matlab codes. Every time I run Oct2py in my script it opens in my processes an octave-cli.exe *32 for each one but does not close it after the process is finished. Since I have multiple uses of it and now wish to have a loop of about a thousand this has become a problem.

Is there some command I can give to close the octave client after the run has been performed? I haven't found any references about this.

Something like:

ans = oc.read_file(filename)
close(octave)

Answer

The exit method of the Oct2Py class will close the underlying Octave session.

ans = oc.read_file(filename)
oc.exit()

The constant spawning of Octave sessions in a loop may itself be a performance bottleneck. it may be worth writing your code such that you can reuse the Oct2Py instance each time through the loop.

octave = Oct2Py()

for filename in filenames:
    # Call Octave command
    output = octave.read_file(filename)

    # Perform any necessary cleanup
Comments