WaitNext
Details
- The eidi are EvaluationObject expressions obtained by submitting expressions for concurrent evaluation using ParallelSubmit.
- WaitNext[{eid1,eid2,…}] returns a list {res,eid,eids}, where res is the result of the finished evaluation, eid is its EvaluationObject, and eids is the list of remaining evaluations.
- The last element in the list produced by WaitNext is suitable for another call to WaitNext.
Examples
open all close allBasic Examples (1)
Submit a number of integer factorizations:
eids = Table[ParallelSubmit[{i}, Length[FactorInteger[2 ^ i - 1]]], {i, 181, 186}]Wait until the fastest one is done:
{res, id, eids} = WaitNext[eids]{res, id, eids} = WaitNext[eids];resFinally, wait for all remaining ones:
WaitAll[eids]Scope (1)
Define a symbol only on the master kernel:
Private`getID := $KernelIDSubmit a number of evaluations using this symbol:
procs = Table[ParallelSubmit[Private`getID], {4}];{res, i, procs} = WaitNext[procs];resReturn the next result, but wrap it in Hold first:
{res, i, procs} = WaitNext[procs, Hold];resApplications (1)
Test
for primality, returning $Failed if it is not prime:
test[n_] := If[PrimeQ[n! + 1], n, $Failed]
DistributeDefinitions[test];Adaptively schedule search jobs until a result has been found or the computation is aborted:
ids = {};res = $Failed;next = 1;
next = 1400;PrintTemporary[Dynamic[next]];
CheckAbort[While[res === $Failed,
While[Length[ids] ≤ $KernelCount,
AppendTo[ids, Composition[ParallelSubmit, test][next++]]];
If[Length[ids] > 0, {res, id, ids} = WaitNext[ids]];
], Null];AbortKernels[];
resProperties & Relations (2)
ParallelTry is essentially implemented in terms of WaitNext:
parallelTry[f_, args_List] := Module[{evs, ev, res = $Failed},
evs = Composition[ParallelSubmit, f] /@ args;
While[res === $Failed && Length[evs] > 0, {res, ev, evs} = WaitNext[evs]];
AbortKernels[];
res
]parallelTry[FactorInteger, Range[10 ^ 20 + 1, 10 ^ 20 + $KernelCount]]Scheduled evaluations no longer needed can be aborted with AbortKernels:
evs = Composition[ParallelSubmit, FactorInteger] /@ Range[10 ^ 20 + 1, 10 ^ 20 + 4]{res, ev, evs} = WaitNext[evs];resAbortKernels[];Possible Issues (2)
No more than $KernelCount evaluations are initially run to find the fastest one:
evs = ParallelSubmit[Pause[#];#]& /@ {0.8, 1.0, 1.2, 1.4, 0.1}{res, ev, evs} = WaitNext[evs];res{res, ev, evs} = WaitNext[evs];resWaitAll[evs]Each evaluation should be waited for exactly once:
evs = ParallelSubmit[{$KernelID, #}]& /@ Range[2]WaitNext[evs]One of the evaluation objects has already completed:
WaitNext[evs]WaitAll[evs]See Also
WaitAll ParallelSubmit ParallelTry EvaluationObject SocketWaitNext
Function Repository: EvaluationObjectState
Related Guides
History
Text
Wolfram Research (2008), WaitNext, Wolfram Language function, https://reference.wolfram.com/language/ref/WaitNext.html.
CMS
Wolfram Language. 2008. "WaitNext." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WaitNext.html.
APA
Wolfram Language. (2008). WaitNext. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WaitNext.html
BibTeX
@misc{reference.wolfram_2026_waitnext, author="Wolfram Research", title="{WaitNext}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/WaitNext.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_waitnext, organization={Wolfram Research}, title={WaitNext}, year={2008}, url={https://reference.wolfram.com/language/ref/WaitNext.html}, note=[Accessed: 12-June-2026]}