2

I'm trying to run a batch of Matlab scripts and somehow it is not working. The code just stays idle and does nothing until timeout.

This is the minimal code

import subproces as sub
cod = 'timeout -k 300 400 matlab -nodisplay -nosplash -r test'.split()
proc = sub.run(cod, stdout=sub.PIPE, stderr=sub.PIPE)

These lines of code just run until reaching the timeout condition, with no values in stdout and stderr.

If I copy these lines inside the terminal, it works perfectly. (the script itself ends up with «exit», therefore after completion it returns to the terminal)

I have already done this similar process with Octave instead, and it works flawlessly.

I have tried to use matlab's python module, but the one I currently have is not compatible with my current Python version, an alternative could be to downgrade Python, but I'm reluctant to do it.

The timeout condition is required because some of these scripts can loop infinitely. I am checking students codes.

Edit: after discussion here, the main culprit appears to be the timeout command, if taken away, the script works.

16
  • What error message do you get? Commented May 1, 2020 at 10:25
  • Pop-up window reads: "MATLAB has encountered an internal problem and needs to close". Then asks me to send a report of it. I while running the script, I tried to see if matlab was open with htop, but I couldn't find it either. Commented May 1, 2020 at 10:45
  • Ok I tried to replicate this in the terminal, and there is no matlab window. It happens withing the python script. I am even more puzzled, the only difference in the script is that the call of the subprocess is within a function, inside within an try/except, and with the check=True flag. But writting the same in terminal leads to no matlab pop-up window. Commented May 1, 2020 at 10:58
  • 1
    @pancho I would add the information that it seems like it's timeout that is causing issues to the question Commented May 1, 2020 at 11:34
  • 1
    The matlab scripts are testing student submissions to exercises, for sure many of them are not well designed (: Commented May 1, 2020 at 11:36

1 Answer 1

1

You can use the timeout argument of subprocess.run:

import subproces as sub
cod = 'matlab -nodisplay -nosplash -r test'.split()
proc = sub.run(cod, stdout=sub.PIPE, stderr=sub.PIPE, timeout=300)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.