ParallelTry[f,{arg1,arg2,…}]
evaluates f[argi] in parallel, returning the first result received.
ParallelTry[f,{arg1,arg2,…},k]
returns a list of the first k results.
ParallelTry
ParallelTry[f,{arg1,arg2,…}]
evaluates f[argi] in parallel, returning the first result received.
ParallelTry[f,{arg1,arg2,…},k]
returns a list of the first k results.
Details and Options
- ParallelTry automatically distributes computations among different available kernels and processors.
- Any evaluations still underway after k results have been received are aborted.
- If the result from evaluating one of the f[argi] is $Failed, then it is ignored.
- If no results other than $Failed are obtained, ParallelTry returns $Failed.
- ParallelTry[f,{arg1,arg2,…}] allocates evaluations of the f[argi] to different kernels and processors, starting with arg1.
- If there are more argi than kernels, then some of the argi will not be tried unless earlier ones return $Failed.
- ParallelTry takes the same DistributedContexts option as ParallelTable.
Examples
open all close allBasic Examples (2)
Find the factorization of a number that is easy to factorize:
ParallelTry[FactorInteger, Range[10 ^ 20 + 1, 10 ^ 20 + $KernelCount]]Nondeterministically find a prime in a given range:
ParallelTry[If[PrimeQ[#], #, $Failed]&, Range[10 ^ 100, 10 ^ 100 + 1000]]Scope (2)
Return the first two successful results:
ParallelTry[If[PrimeQ[#], #, $Failed]&, Range[10 ^ 100, 10 ^ 100 + 1000], 2] - 10 ^ 100If too few results are available, the result may be shorter than requested:
ParallelTry[If[PrimeQ[#], #, $Failed]&, Range[10 ^ 100, 10 ^ 100 + 1000], 4] - 10 ^ 100If no successful results are found, ParallelTry returns $Failed:
ParallelTry[If[PrimeQ[#], #, $Failed]&, Range[10 ^ 100, 10 ^ 100 + 100]]Options (5)
DistributedContexts (5)
By default, definitions in the current context are distributed automatically:
remote[x_] := {$KernelID, x ^ 3}ParallelTry[remote, {1, 2, 3, 4}]Do not distribute any definitions of functions:
local[x_] := {$KernelID, x ^ 2}ParallelTry[local, {1, 2, 3, 4}, DistributedContexts -> None]Distribute definitions for all symbols in all contexts appearing in a parallel computation:
a`f[x_] := {$KernelID, x}ParallelTry[a`f, {1, 2, 3, 4}, 2, DistributedContexts -> Automatic]Distribute only definitions in the given contexts:
b`g[x_] := {$KernelID, -x}ParallelTry[b`g, {1, 2, 3, 4}, DistributedContexts -> {"a`"}]Restore the value of the DistributedContexts option to its default:
SetOptions[ParallelTry, DistributedContexts :> $DistributedContexts]Applications (5)
Find a random prime in parallel:
ParallelTry[
(While[!PrimeQ[p = RandomInteger[{10 ^ 100, 10 ^ 101}]]];p)&, Range[$ProcessorCount]]Find an irreducible polynomial with a random search on each parallel kernel:
randPoly[n_Integer ? Positive, x_, p_] := Expand[Fold[x * #1 + #2&, 1, RandomInteger[{0, p - 1}, n]]]findOne[n_, x_, p_ ? PrimeQ] := Module[{poly},
While[!IrreduciblePolynomialQ[poly = randPoly[n, x, p], Modulus -> p], Null];
poly]ParallelTry[findOne[100, x, 2]&, Range[$KernelCount]]Search for a Mersenne prime starting at a given prime exponent:
search[n0_, dn_] :=
Module[{n = n0}, While[!PrimeQ[2 ^ Prime[n] - 1], n += dn];n]With[{dn = $KernelCount, n0 = 500},
n = ParallelTry[search[n0 + #, dn]&, Range[0, dn - 1]];
{n, Prime[n]}
]Try different methods for minimization and return the first one that succeeds:
ParallelTry[
{#, FindMinimum[E^Sin[50 x] + Sin[60 E^y] + Sin[70 Sin[x]] + Sin[Sin[80 y]] - Sin[10 (x + y)] + (1/4) (x^2 + y^2), {x, y}, Method -> #]}&, {"Gradient", "ConjugateGradient", "InteriorPoint", "QuasiNewton", "Newton"}]Try different symbolic methods of summation and return the first one to succeed:
ParallelTry[(s = Sum[(a^k (-1 + a - k)/(1 + k) k!), k, Method -> #];If[Head@s === Sum, $Failed, s])&, {"Polynomial", "PolynomialExponential", "HypergeometricTermGosper", "PolynomialTrigonometric"}]In this case, only one method succeeded:
ParallelMap[(s = Sum[(a^k (-1 + a - k)/(1 + k) k!), k, Method -> #];If[Head@s === Sum, $Failed, s])&, {"Polynomial", "PolynomialExponential", "HypergeometricTermGosper", "PolynomialTrigonometric"}]Properties & Relations (5)
ParallelTry generally returns the first successful result received:
ParallelTry[(Pause[#];#)&, {4, 3, 2, 1}]ParallelTry works like ParallelMap, but returns only one of the results:
ParallelMap[f, {a, b, c, d}]ParallelTry[f, {a, b, c, d}]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]]Functions defined interactively are automatically distributed to all kernels when needed:
ftest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]ParallelTry[ftest, Range[1275, 1285]]Distribute definitions manually and disable automatic distribution:
gtest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]DistributeDefinitions[gtest];ParallelTry[ftest, Range[1275, 1285], 3, DistributedContexts -> None]For functions from a package, use ParallelNeeds rather than DistributeDefinitions:
Needs["FiniteFields`"]Map[FieldSize, Table[GF[2, i], {i, 10}]]ParallelNeeds["FiniteFields`"]ParallelTry[FieldSize, Table[GF[2, i], {i, 10}], 4]Possible Issues (3)
If there are more expressions than kernels, some will never be tried:
ParallelTry[(Print[#];Pause[RandomReal[]];$KernelID)&, Range[$KernelCount + 1]]A function used that is not known on the parallel kernels may lead to sequential evaluation:
ptest[n_] := If[PrimeQ[2 ^ n - 1], {n, $KernelID}, $Failed]ParallelTry[ptest, Range[20, 40], DistributedContexts -> None]Define the function on all parallel kernels:
DistributeDefinitions[ptest]The function is now evaluated on the parallel kernels:
ParallelTry[ptest, Range[20, 40], DistributedContexts -> None]Definitions of functions in the current context are distributed automatically:
qtest[n_] := If[PrimeQ[2 ^ n - 1], {n, $KernelID}, $Failed]ParallelTry[ptest, Range[20, 70], 2]Definitions from contexts other than the default context are not distributed automatically:
ctx`ptest[n_] := If[PrimeQ[2 ^ n - 1], {n, $KernelID}, $Failed]ParallelTry[ctx`ptest, Range[20, 40]]Use DistributeDefinitions to distribute such definitions:
DistributeDefinitions[ctx`ptest]ParallelTry[ctx`ptest, Range[20, 40]]Alternatively, set the DistributedContexts option to include all contexts:
cty`ptest[n_] := If[PrimeQ[2 ^ n - 1], {n, $KernelID}, $Failed]ParallelTry[cty`ptest, Range[20, 70], 2, DistributedContexts -> Automatic]See Also
Related Guides
Related Workflows
- Run a Computation in Parallel
Text
Wolfram Research (2008), ParallelTry, Wolfram Language function, https://reference.wolfram.com/language/ref/ParallelTry.html (updated 2010).
CMS
Wolfram Language. 2008. "ParallelTry." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2010. https://reference.wolfram.com/language/ref/ParallelTry.html.
APA
Wolfram Language. (2008). ParallelTry. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ParallelTry.html
BibTeX
@misc{reference.wolfram_2026_paralleltry, author="Wolfram Research", title="{ParallelTry}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/ParallelTry.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_paralleltry, organization={Wolfram Research}, title={ParallelTry}, year={2010}, url={https://reference.wolfram.com/language/ref/ParallelTry.html}, note=[Accessed: 12-June-2026]}