interruptedWhile ADDENDUM
- To: mathgroup at smc.vnet.net
- Subject: [mg24615] interruptedWhile ADDENDUM
- From: Daniel Reeves <dreeves at eecs.umich.edu>
- Date: Fri, 28 Jul 2000 17:23:51 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Well, it worked fine on small tests but I failed to notice that it would
run into $IterationLimit eventually. Here's a better version:
SetAttributes[interruptedWhile, HoldRest];
interruptedWhile[interruptFreq_, interruptBody_, whileTest_, whileBody_]
:=
While[
TimeConstrained[
While[whileTest, AbortProtect[whileBody]],
interruptFreq];
If[whileTest, Evaluate[interruptBody]; True, False]
]
> (* Like a While loop except for the 1st 2 args, which say: every so many
> seconds pause and execute some code, presumably with the purpose of
> announcing some partial results or the status of the computation.
> *)
> SetAttributes[interruptedWhile, HoldRest];
> interruptedWhile[interruptFreq_, interruptBody_, whileTest_, whileBody_]
> :=
> Module[{compLoop, goComp},
> compLoop := While[whileTest, AbortProtect[whileBody]];
> goComp := TimeConstrained[compLoop, interruptFreq,
> Evaluate[interruptBody]; goComp];
> goComp;
> ]
>
>
> Here's an example of using it:
>
> i = 1;
> numFound = 0;
> interruptedWhile[3,
> Print["Checked 1 thru ", i - 1, " and found ", numFound,
> " primes (expected ", PrimePi[i - 1], ")..."],
> i <= 1000000,
> If[PrimeQ[i], numFound++];
> i++;
> ]
>
> Of course, you can manually interrupt and check on a computation from the
> front-end, so this is most useful for a really long computation that you
> have running in the background. In those cases, I often like to have the
> program send periodic emails. So while I'm at it, I'll paste in code to
> do that (of course this only works on Unix/Linux):