kill an external process (simulation ) if it is taking too long
2 次查看(过去 30 天)
显示 更早的评论
Hey everyone,
even i am using the same stratergy ( https://in.mathworks.com/matlabcentral/answers/300633-external-program-run-kill-from-matlab ) to call an external function and pause for several minutes then kill the process.
it is fine when my external process get into some infinite numerical calculations or it gets strucks.
but my problem is it waits for "n" amount of time even for process that finishes early that n.
can any1 suggest any approach where it waits and kills only when the process gets struck.
it should not wait for the good ones.
2 个评论
Rik
2019-12-16
Is it possible for your external program to create a flag file? Then you could loop a short pause and a check for the existence of the file.
回答(2 个)
Guillaume
2019-12-18
killtimer = timer('ExecutionMode', 'singleshot', 'StartDelay', 5, 'TimerFcn', @killtask); %executes after 5 seconds
yourcodetostartyourprogram
start(killtimer); %start the timer
%...rest of your code as usual
and a seperate function
function killtask(~, ~)
try %wrap in a try catch so that if the program is already terminated we don't throw an error
yourkillcode
end
end
3 个评论
Guillaume
2019-12-19
I don't understand why people ask a question, get answers that solve the very question they ask, then come back with "actually..." and different question. Why didn't you ask that in the first place?
If you want to interrupt a loop after a certain time simply put a time limit as part of the loop condition, something like:
tstart = tic;
while toc(start) < yourdelay
dosomething
end
Walter Roberson
2019-12-18
If you are using MS Windows then you can use .NET as described in https://www.mathworks.com/matlabcentral/answers/418173-run-programm-in-background-without-using-start#answer_335999 . You could loop checking the status at short interals.
If I recall correctly, I have read that you can create callbacks for .NET code; if that is correct, then perhaps it would be possible to create a callback when the process finishes normally; you could use that together with a timer() so as to kill the process if it runs too long.
5 个评论
Walter Roberson
2019-12-19
See the link I already posted as it shows creating the process.
I do not have a Windows MATLAB installed to test with so I cannot write complete code myself.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!