6

In exploring Working with DynamicModule: Tracking the progress of Initialization, I came across this. The following usually shows a value for x of 300000 to 400000 after initialization. It varies each time.

DynamicModule[{x}, Dynamic[x], Initialization :> (x = 1;
   Do[x = i, {i, 10000000}];
   x = 2;
   Do[x = i, {i, 10000000}];
   x = 3;)]

If the option SynchronousInitialization -> True is set, a similar thing happens, although the displayed value for x ranged from 18000 to 9900000. On the other hand, if SynchronousInitialization -> False is set, the displayed value is 3, as expected.

Is this a bug, or is it to be expected, due to some aspect of the dynamic updating system I'm not aware of?

Version: 10.0.0, 9.0.1, and 8.0.4; Max OSX 10.9.4.

Michael E2
  • 235,386
  • 17
  • 334
  • 747

1 Answers1

7

SynchronousInitialization -> True causes dynamic evaluations to occur on the preemptive link. This locks up the notebook front-end for the duration of an evaluation. To avoid locking up the front-end indefinitely, there is a default timeout of six seconds:

CurrentValue[EvaluationNotebook[], DynamicEvaluationTimeout]
(* 6. *)

The values we see for x are simply those that have been set prior to the timeout.

On my machine, it takes about 25 seconds to perform the complete initialization. If I set the timeout to a value higher than that, then I get the expected result:

SetOptions[EvaluationNotebook[], {DynamicEvaluationTimeout -> 60}]

The original motivation for this question aside, it is generally inadvisable to perform long-running evaluations synchronously on the preemptive link. It is especially dangerous to set the timeout to infinity or a large number because it disables the ability to terminate a runaway calculation.

WReach
  • 68,832
  • 4
  • 164
  • 269
  • Thanks. I think this must be right. When I first noticed, I had timed the do-loop at just under a second and substituted it for Pause[1]. This morning it's taking 2.6 sec. and the complete initialization code on its own takes 5.3 sec. If I put SessionTime[] in cells before and after the DynamicModule, the difference is slightly over 6 sec. I knew about the time constraint, but thought I was within that time. I must have been mistaken last night about the timing of the loop. I also thought there would be a warning if it timed out. – Michael E2 Sep 18 '14 at 11:34