New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Practical advantage of promises in shiny? #52
Comments
|
Hi there! It's not true that the event loop is blocked while a promise is pending. That really would be pretty useless However to try to avoid forcing the app author to deal with race conditions, within a single session we suspend I/O with the browser until all pending promises have completed. While this does affect the kind of thing you're trying to do, it at least lets multiple sessions make progress simultaneously. If you know what you're doing (and it sounds like you do!) then you can opt out of this session-level synchronization with a technique I posted in a comment on this issue: #23 |
|
Ok, thank you. On the internal design, just to understand, is there a single event loop handling all sessions, or each session gets its own loop on a separate thread/process? I think I get the overall design now. In python terms, it's more like tornado, rather than flask. |
|
There's a single event loop handling all sessions. The event loop itself is never suspended. When Shiny sessions have pending promises (that they know about) they just set a flag on themselves to queue input/output until the flag is unset. |
|
@jcheng5 and I assume that Shiny learns about the pending futures because the server function returns them? If that's the case, as it seems to be from other posts that return NULL from the server function, how is it achieved that with "fire and forget" futures the then() function actually executes in the main thread? to do so,
It's quite impressive as a design, but there's a lot of magic happening and I am trying to understand it better. |
|
Sorry for the months-late reply, I missed your comment until now. The logic you're interested in is here: Lines 422 to 456 in 9ebad6d
Essentially, when |
|
I've just recently stumbled upon this thread while trying to wrap my head around Shiny and Plumber's auto-magical handling of Future objects. One piece that I'm stuck on is how
Once Shiny enters its main event loop, isn't there always R code present on the main execution stack? That is, the loop code itself is currently executing, and so I'm struggling to figure out how the I've searched for the workaround Clearly I'm missing something w.r.t. R's handling of background tasks ... perhaps there's a more in-depth guide somewhere on the scheduling internals of later and promises? |
|
@mmuurr You’re right, this appears to be a paradox. It works because when shiny or plumber are blocking the console, it’s actually httpuv::service that’s repeatedly being called in a while loop, and httpuv::service calls later::run_now. |
|
@jcheng5, great, thanks for the tip there ... I now see that BTW -- in this Shiny code line found here: timeout <- max(1, min(maxTimeout, timerCallbacks$timeToNextEvent(), later::next_op_secs()))... are units being mixed incorrectly?
|
|
I think you might be right about the mixed units, it looks like that code was written carefully and then refactored less carefully (by me). If you happen to feel inclined to submit a PR, I’m sure @wch would appreciate it! |
|
re: units; will do. Thanks for the dialog; very helpful! |
Sorry this is more of a question rather than an issue, but I could not get a definitive answer on other channels.
I am experienced with React and async under python. I am new to the world of R and Shiny.
I noticed from the documentation that a future must be completed when the iteration of the event loop is completed. There's thus the equivalence of a synchronization barrier that does not let the event loop proceed further in parsing events until all the future are resolved.
My question is: what is the final advantage of using an async approach in shiny? If I have a process that can take, say, up to two minutes (e.g. a network request timeout, or a heavy calculation), the event loop will be unable to process any further input for those two minutes. The only possible advantage is if multiple futures are spawned.
My goal is to replace HTML elements before and after an async operation, but it's a testbed for training in Shiny, not an actual problem I have to solve. I am not therefore looking for a workaround or solution, rather to understand the general concepts and preferred approaches.
Thanks
The text was updated successfully, but these errors were encountered: