Closed schloerke closed 2 years ago
I think that "tick" isn't the right way to describe how {later} works. In Javascript, the word "tick" does make sense, because the event loop runs all the callbacks which have come due in the tick, then it releases control to the browser to run whatever non-JS browser stuff that needs to be run, and then it goes back to executing the event loop -- that's the next tick. If, in the first run of the event loop, any callbacks are scheduled with setTimeout(fn, 0)
, then those callbacks will run in the next tick.
With {later}, once it starts executing callbacks in the event loop, it just keeps executing them as long as there are any whose time has come due. It doesn't grab the set of currently-due callbacks, execute them, release control, and then start executing again.
Found while debugging https://github.com/rstudio/shiny/issues/2196
Reprex app:
In the demo app, 500
future_promise()
submissions are created. Once the first 3 jobs have been submitted, the remaining 497 jobs are attempting to ask "Am I able to proceed?" / "Are there any future workers available?".Once the workers run out, the answer should stay the same for the remainder of the computation tick. This PR reduces that question count by 496 (497 - 1).
The value will reset on the next tick.
While faster in total computation, I believe it could be even faster. I'd like to know what the current loop iteration is, i.e.
loop tick #21
. So that if the loop has a different tick number we could use that information, rather than relying on later::later(delay = 0) to execute. But if the reset is being done in the beginning of the work queue, then it might be fine.