How to set timeout to a function

147 visualizaciones (últimos 30 días)
Javier Checa
Javier Checa el 16 de Abr. de 2021
Comentada: Jan Kappen el 13 de Sept. de 2023
Hi everyone, I need to stop the execution of a function if finish a specific time. Specifically the part of the program where i need to implement it is when i call a function to create a regressor:
[trainedModel, validationRMSE] = trainRegressionModelLinear(T, KFolds);
I have left the program running and it never finish the function so i wanted to exit the function and continue the rest of the program. The function works because i change the data "T" that i insert in the function and i get a trainedModel with its value but with long data T i think no works.
There is a example that i need to do but with try- catch doesnt work:
try
[trainedModel, validationRMSE] = trainRegressionModelLinear(T, KFolds);
catch (*time out finish*)
validationRMSE = "Error";
trainedModel = [];
end
I find some information how said that i need to use parallel pool, batch(), parfeval() or a timer but i dont understand what function is useful for my situation. I already use parallel pool with 4 workers because is better to train regressor but to apply the time out no idea.
Thanks so much for the help and sorry if I have explained badly because the english is not my native language.

Respuesta aceptada

Edric Ellis
Edric Ellis el 19 de Abr. de 2021
You could use parfeval to do this, but I'm not sure how you might fit that in with the rest of your parpool usage. Here's the simplest case where you use wait with a single future:
fut = parfeval(@pause, 0, 20); % Dummy function - you'd use your training function here
ok = wait(fut, 'finished', 4); % Wait for up to 4 seconds for 'fut' to finish
if ~ok
disp('Did not finish in time.')
else
disp('Did finish in time.')
end
You could use fetchNext to do something more sophisticated with multiple futures, as that also takes a timeout parameter.
  6 comentarios
Vincent
Vincent el 13 de Sept. de 2023
@Jan Kappen I am using matlab 2019a
Jan Kappen
Jan Kappen el 13 de Sept. de 2023

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel Computing Fundamentals en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by