Why is the timeout not triggering using Wait(F, state, timeout) for a parallel process?

10 次查看(过去 30 天)
Hello everyone.
I have to implement a timeout function for a simulation sweep, so that I can stop non-converging solutions when the computation times gets too long and then I just skip to the next set of parameters.
I however noticed that the timeout was not triggering, so I made a simpler code to test it out.
The parallel process simply waits 5 seconds, and I set a timeout at 4s.
From the documentation:
tf = wait(F,state,timeout) blocks execution for a maximum of timeout seconds.
If each element of the Future array F finishes before timeout seconds elapse, tf is true. Otherwise, tf is false.
So after f is called, the code waits for F to finish or for 4 seconds to elaps, at which point it will return either 1 or 0, but the code always runs for 5 seconds+, and timeout is always true.
Am I missing something?
Thank you in advance
timeout = 4;
startTime = tic;
f = parfeval(@t, 1, 5);
timeout = wait(f, 'finished', timeout);
elapsedTime = toc(startTime);
disp(elapsedTime);
if ~timeout
disp('Did not finish in time.')
cancel(f);
else
disp('finished on time.')
out = fetchOutputs(f);
end
function out = t(i)
pause(i)
out = i;
end
  1 个评论
Edric Ellis
Edric Ellis 2024-11-25
Which release and which OS are you seeing a problem on? I just tried R2022b and R2024b on Linux, and things worked correctly. Have you turned off "Auto create parallel pool"? In that case, the parfeval body can run in the client, and when that happens, the wait always has to wait for the execution to complete.

请先登录,再进行评论。

回答(1 个)

Hitesh
Hitesh 2024-11-25
I too have encountered the same issue, the workaround that worked for me was updating the MATLAB to R2024b version. I have executed your sample code and it is giving the expected result. I have attached an image of the output for your reference.
For more information regarding the "wait" function, kindly refer to the following MATLAB documentation:

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by