- a timer object
- side-effect of a conditional breakpoint (requires line numbers of the loop)
- save
Stop an infinite loop after a certain amount of time
19 views (last 30 days)
Show older comments
How can I stop a function after a certain amount of time has passed if i do not have access to editing the function? Let's say a function fun contains an infinite loop, and I want to run it for thirty seconds before terminating it and getting whatever outputs it may have assigned, how would I do that?
3 Comments
Rik
on 3 Dec 2017
If you have no access to the code itself, you can't stop execution and get output values. If it is an internal function (or p-code) I doubt the pause button will work.
per isakson
on 3 Dec 2017
Edited: per isakson
on 3 Dec 2017
A small experiment
An infinite loop
function cssm()
while true
a = 17;
end
end
Set a conditional break-point in the loop. An appropriate line number is needed.
>> dbstop in cssm.m at 4 if stop_cssm
where
function tf = stop_cssm
persistent count
if isempty( count )
count = 1;
end
count = count + 1;
if count >= 1e5
tf = true;
else
tf = false;
end
end
Start cssm. After 1e5 calls to stop_cssm the break-point halts the loop.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/187271/image.png)
.
My idea is that instead of returning true, stop_cssm shall read the variables of interest in cssm an save the data to a mat-file. Possibly the function now is simpler to use than a timer. This approach will slow down the loop.
Answers (2)
Hafsa Asad
on 12 Sep 2018
Edited: Walter Roberson
on 12 Sep 2018
how about this for a 15 second-limit?
tic
while toc<15
% do stuff
end
1 Comment
Walter Roberson
on 12 Sep 2018
Notice from the question "Let's say a function fun contains an infinite loop," and "i do not have access to editing the function"
In order to interrupt an infinite loop, you need to either edit the function of the loop or else use one of the techniques I described in my Answer that can shut down processing after a given time. Unfortunately those methods of shutting down processing do not leave any data to be retrieved.
Example:
function result = alog50
t = 0;
k = 0;
while t < 50
k = k + 1;
t = t + 1/k;
end
result = k;
Now, mathematically this is the harmonic sum, which goes out to infinity, so it passes through every finite value. There is definitely a k for which the harmonic sum reaches 50 -- the k value would be roughly 2.1E21 .
However, once you get past k roughly 1E16, then 1/k is less than eps(t), and the floating point stops accumulating when added one by one like this. The mathematical sum has a solution, but the floating point sum has no solution: once you reach k = 1E16 or so, each additional value adds 0 in floating point terms, and the sum never reaches 50.
So, mathematically this function is well defined, but in floating point it is an infinite loop.
You might find through experience running it that it is taking much longer than you want. It is plausible that you might want to let it run for 15 seconds and then give the result it has found so far. But under the hypothesis of the question, you cannot edit the code. So what to do?
You can use one of the techniques I wrote about, but when the time is reached and the task is canceled, it is much like pressing control-C: whatever is being done right then is killed. You cannot return the result "so far" because result has not been assigned to.
If the code had been written as
function k = alog50
t = 0;
k = 0;
while t < 50
k = k + 1;
t = t + 1/k;
end
then it might sense to return the k "so far". But we do not have access to edit the code (hypothetically)... and MATLAB does not provide any way to nudge an executing function to give up a loop early.
There are things you can do with the cooperation of the function . Like having a timer() running that sets a shared variable that the loop examines to see if it is being asked to exit. But the user does not have that cooperation.
Walter Roberson
on 3 Dec 2017
There is no way to do that.
There are only a few ways to run a function for a limited time without the active cooperation of the function:
- use parfeval() from the Parallel Processing Toolbox, and cancel() the job
- use batch() from the Parallel Processing Toolbox, and cancel() the job
- use system() to start a second copy of MATLAB, and use taskmgr (MS Windows) or kill (Mac or Linux) to cancel the job
- Mac or Linux only: start a second copy of MATLAB in which limit has been set to limit the CPU or elapsed time to the appropriate limit
none of these leave any values around to be collected.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!