Repository navigation
allow rstudioapi to communicate with RStudio from child procs - #155
Conversation
| #' \dontrun{rstudioapi::verifyAvailable()} | ||
| isAvailable <- function(version_needed = NULL) { | ||
|
|
||
| if (isChildProcess()) |
There was a problem hiding this comment.
I feel like this might be too much magic. There may be existing code out there that relies on isAvailable to figure out if it's running in RStudio's main session. As written IPCs also require the synchronization of two independent polling loops (one in the package, one in RStudio), so are going to be expensive to call from the child process, which the caller might need to take into account. How would you feel about a separate function to check if the API is available in the parent?
There was a problem hiding this comment.
That's a good point. My original intention was that a script a user wrote to run in the main R rsession should "just work" (at least as far as rstudioapi calls go) when run as a child process as well, but the distinction is probably necessary in some contexts.
There was a problem hiding this comment.
Yeah. I think we should have a separate function which asks "am I in a child R process being run by RStudio?", which can just be a more official/exported version of isChildProcess. The tidyverse team has asked for how to determine this deterministically a number of times!
The site we're discussing here can just get a new parameter that indicates whether it should ask the parent if necessary (default to existing behavior, which is to only look at the main R session).
I think ordinary API calls being magical is okay as it makes code more portable.
| saveRDS(data, file = tmp) | ||
| file.rename(tmp, request) | ||
|
|
||
| # loop until response is ready (poll) |
There was a problem hiding this comment.
A lot of API commands are fire and forget (they don't return any useful value). In those cases, could we indicate by some means that the command is to be sent to the parent process and execution is to proceed without waiting for a response? I don't think we need to wait for all of these polling loops to line up if the caller doesn't need the response (errors be damned).
There was a problem hiding this comment.
This would complicate things a bit.
If the child process can submit multiple commands without waiting for a response, then we'll need a proper queue that the parent + child process can synchronize over, rather than just the "single file" approach. We could probably build something on top of an existing CRAN package but it could be substantially more work + would require more dependencies for rstudioapi (but maybe that's fine).
We'd also need some way to encode, for each rstudioapi function, whether it has an "interesting" return value.
It seems unlikely that users would want to make a large number of rstudioapi calls in succession from one script, so I'm not sure if optimizing for this particular facet is worth it.
There was a problem hiding this comment.
It seems unlikely that users would want to make a large number of rstudioapi calls in succession from one script
I do think this is going to be common. One use of this feature will be to have child R sessions reporting progress to the parent session; for example, knitr could detect when it's being run as a child RStudio process and use rstudioapi to draw progress bars in RStudio as the knit proceeds, instead of its current ASCII bars. knitr can emit dozens of updates in the space of a second; waiting for all of the IPC files to sync and clear will cause the progress bar update time to exceed the knit time!
That said, we could take this as-is now and implement async later if we need it.
| data <- list(secret = secret, call = call) | ||
| tmp <- tempfile(tmpdir = dirname(request)) | ||
| saveRDS(data, file = tmp) | ||
| file.rename(tmp, request) |
There was a problem hiding this comment.
What should we do here if the request file already exists?
There was a problem hiding this comment.
This shouldn't ever happen (at least in the current architecture) since RStudio won't allow the child R process to proceed from the polling loop below until that file has been removed:
|
Taking this as-is for now but we may need to consider more performant IPC (especially if we want to allow for things like progress bars or other tools that will require more frequent communication) |
|
Will ipc be a good option for this purpose? Simple case can use file source, more performant case can use redis source. |
|
I don't think |
Re: rstudio/rstudio#4490.
This PR implements a (somewhat primitive) mechanism for performing IPC with the parent RStudio session. A pair of files are used to transfer 'requests' / 'responses'; the requests made by
rstudioapiare just serialized R calls that can be evaluated in the main R session, and the responses can later be read after they are written by the parent session.This PR communicates through plain files for a couple reasons:
Since the relationship here is 1-1, and there's no notion of a "queue" of actions, just using a regular file keeps things simple.
R's own solutions for this don't appear appropriate:
socketConnection()are network sockets, and so require a host + port and the host of issues that accompany those.fifo()seem like an option, but unfortunately (as far as I can tell) are unreliable on Windows.We could likely consider farming this out to a separate R package that can handle connections like these more gracefully, but given our small requirements list it seems okay to proceed without dependencies and just use plain old files.
Exposing the rsession's guts / C routines to child processes is likely to be challenging, so I wanted to avoid that route.
The main downside of this approach is we need to poll the filesystem rather than listen or wait for something like an OS signal, but that seems like it's worth paying given the downsides of other options. (If we found a low-dependency CRAN package that gave us the lower-level connection bits we might instead want to depend on that)