简体   繁体   English

使用 python 子进程和超时命令运行 Matlab 脚本

[英]Run Matlab script with python subprocess and timeout command

I'm trying to run a batch of Matlab scripts and somehow it is not working.我正在尝试运行一批 Matlab 脚本,但不知何故它不起作用。 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.这些代码行一直运行直到达到超时条件,在 stdout 和 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) (脚本本身以«exit»结束,因此完成后返回终端)

I have already done this similar process with Octave instead, and it works flawlessly.我已经用 Octave 完成了类似的过程,并且它完美无缺。

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.我曾尝试使用 matlab 的 python 模块,但我目前拥有的模块与我当前的 Python 版本不兼容,另一种方法可能是将 ZA7F5F35426B927411FC9231B56382173 降级,但 I'm.

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.编辑:在这里讨论之后,罪魁祸首似乎是超时命令,如果拿走,脚本就可以工作。

You can use the timeout argument of subprocess.run :您可以使用subprocess.runtimeout参数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM