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.

pancho
  • 201
  • 1
  • 7
  • What error message do you get? – Daniel May 01 '20 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. – pancho May 01 '20 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. – pancho May 01 '20 at 10:58
  • Still trying to replicate this, and in the script it will output the error message just if I ctrl+c right after it starts executing subprocess.run (if I wait 5 seconds, then it doesn't happens). Maybe it briefly opens Matlab and then "abandons" it? – pancho May 01 '20 at 11:11
  • Is the file `test` on the default Matlab path? – rinkert May 01 '20 at 11:11
  • No it isn't, it is in the same folder from which I am running the python script. But if I run the same command in the terminal, it works without issues, shouldn't subprocess.run replicate this behavior? on the other hand, the equivalent code in Octave doesn't require to include the file in the default path. – pancho May 01 '20 at 11:13
  • Nonetheless I just copypasted the file in the default Matlab path and subprocess.run also failed. – pancho May 01 '20 at 11:16
  • I agree, maybe it's not the issue, but it's an easy thing to rule out. And what if you surround `test` by double quotes? – rinkert May 01 '20 at 11:16
  • the behavior doesn't changes. On a similar spirit I just used a non-existant file name, and it also does the same. Maybe it is just not finding the script – pancho May 01 '20 at 11:20
  • I assume that the command does work from python without the `timeout`? – rinkert May 01 '20 at 11:24
  • Just tested it, it does flawlessly. It is hard to debug since when using timeout (and taking away the flags stdout, stderr) I don't get any output. Somehow the timeout command is freezing it. – pancho May 01 '20 at 11:27
  • 1
    @pancho I would add the information that it seems like it's `timeout` that is causing issues to the question – Paolo May 01 '20 at 11:34
  • By the way, this `The timeout condition is required because some of these scripts can loop infinitely. ` tells me that your MATLAB code is not well designed – Paolo May 01 '20 at 11:34
  • 1
    The matlab scripts are testing student submissions to exercises, for sure many of them are not well designed (: – pancho May 01 '20 at 11:36
  • I guess you can get the timeout you want using the `timeout` argument of `subprocess.run` ([see here](https://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout)). – rinkert May 01 '20 at 11:36
  • This is working ! Another issue is happening tho. After timeouting the code, the terminal starts rejecting any inputs, I can get out of python with ctrl+D, but even then, the terminal is still unresponsive. Maybe this is not an issue when running a python script instead of directly on the terminal, if it doesn't, probably I will need to make another post. Please post this as an answer so I can declare it solved ! – pancho May 01 '20 at 11:45

1 Answers1

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)
rinkert
  • 6,593
  • 2
  • 12
  • 31