fetchNext
Retrieve next available unread FevalFuture outputs
Syntax
[idx,B1,B2,...,Bn] = fetchNext(F)
[idx,B1,B2,...,Bn] = fetchNext(F,TIMEOUT)
Description
[idx,B1,B2,...,Bn] = fetchNext(F)
waits for an unread FevalFuture in the array of futuresF
to finish, and then returns the linear index of that future in arrayF
asidx
, along with the future’s results inB1,B2,...,Bn
. Before this call, the'Read'
property of the particular future isfalse
; afterward it istrue
.
[idx,B1,B2,...,Bn] = fetchNext(F,TIMEOUT)
waits no longer thanTIMEOUT
seconds for a result to become available. If the timeout expires before any result becomes available, all output arguments are empty.
If there are no futures inF
whose'Read'
property isfalse
, then an error is reported. You can check whether there are any unread futures usinganyUnread = ~all([F.Read])
.
If the element ofF
which has become finished encountered an error during execution, that error will be thrown byfetchNext
. However, that future’s'Read'
property is settrue
, so that any subsequent calls tofetchNext
can proceed.
Examples
Request several function evaluations, and update a progress bar while waiting for completion.
N = 100;foridx = N:-1:1% Compute the rank of N magic squaresF(idx) = parfeval(@rank,1,magic(idx));end% Build a waitbar to track progressh = waitbar(0,'Waiting for FevalFutures to complete...'); results = zeros(1,N);foridx = 1:N [completedIdx,thisResult] = fetchNext(F);% store the resultresults(completedIdx) = thisResult;% update waitbarwaitbar(idx/N,h,sprintf('Latest result: %d',thisResult));enddelete(h)
Tips
ThefetchNext
function returns the linear index of the future from its array. If instead, you need the subscript values of a multidimensional array, you can use theind2sub
function to convert the values.