How do I wait for a function to complete in some stipulated time or move on?
Show older comments
I have two functions A and B. I want to run B after A but only if A completes execution within 10 secs. In case A is taking more than 10 secs then I would like to move on with B.
How do i achieve this?
I have tried following and am looking for a better approach.
counter = 0;
% A_resp - bool returned by A when finished
while ( ~A_resp && counter < 100)
pause(0.1);
counter = counter + 1;
end
% B is here
3 Comments
Aaron Pedersen
on 4 Apr 2020
Edited: Aaron Pedersen
on 4 Apr 2020
doesnt work... :(
I would use matlabs CPU time funtion for this. (https://www.mathworks.com/help/matlab/ref/cputime.html)
without knowing how your functions look im not entierly sure how to implement, but i think it would be something like this:
%inside A
tStart = cputime;
% executes computation for A{
tnow = cputime;
if tnow > (tStart+10)
%call func B
return %exit A
end
This should work if A is an iterative function. If you can have A execute and while it is executing time fn A in parallel, you could use a similar approach to your while loop but with cputime. However im not sure on the implementation of this as I have no experiance with the parallel proccesing toolbox.
%worker 1 executing A
%worker 2(timer) waiting for timeout
while ( ~A_resp && tNow < (tStart+10))
%doNothing, let A execute
end
%kill worker 1
%kill timer (worker 2)
%call B
Otherwise the clock function or tic then calling "tNow = toc" over and over could work similarly.
%inside A
%for stuff in A:
tic %at start of a
%do A stuff
t =toc;
if t>10
return %exit a
end
%end A
Hope that helps.
Walter Roberson
on 4 Apr 2020
cputime() and clock() can only work with the cooperation of the function being called: the function itself has to volunteer to give up control after the time limit.
Aaron Pedersen
on 4 Apr 2020
Yes, the more I look into this the more I realize that this is beyond my skill in matlab. Many thanks.
Answers (1)
Walter Roberson
on 4 Apr 2020
1 vote
MATLAB has some undocumented method of restricting CPU time that it uses for MATLAB Online and for Cody. I have not been able to figure out yet how it does that.
Aaron Pedersen suggests using cputime() or clock() within the function to test whether it has reached its limit yet. That works okay for functions that deliberately cooperate... and which do not accidentally get stuck on something while they are not checking time... and which do not end up buried inside LAPACK or symbolic toolbox or similar that do not check timed or otherwise cooperate on being limited.
The only documented way of limiting time on something that is not actively cooperating (and is not getting stuck), is to use Parallel Computing Toolbox to create a "future" such as by parfeval() to evaluate the task in a separate process, and to monitor the status, because futures can be cancel()'d while they are executing.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!