Skip to content
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

RStudio jobs support #256

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

RStudio jobs support #256

wants to merge 4 commits into from

Conversation

gaborcsardi
Copy link
Member

From callr:

fun <- function() {
  for (i in 1:10) {
    cat(paste(i, "\n"))
    prog <- paste0("# ---- ", Sys.getenv("RSTUDIO_API_JOB_ID"),
                   " progress ", i*10, "%\n")
    cat(prog)
    Sys.sleep(1)
  }
}

r <- callr::r_bg(fun, stdout = "|rstudio", stderr = "|rstudio",
                 rstudio_job = TRUE, 
                 rstudio_job_options = list(name = "bgr", progress = TRUE))

rec1

Running a shell script from processx, save this as test.sh first, then run the R code:

#! /bin/bash

for i in `seq 1 10`
do
  sleep 1
  echo $i
  echo "# ---- $RSTUDIO_API_JOB_ID progress ${i}0%"
done
p <- process$new(
  "./test.sh", 
  rstudio_job = TRUE, 
  rstudio_job_options = list(nam = "myjob", progress = TRUE)
)

rec2

Glitches that could be improved with some changes in RStudio and rstudioapi:

  • Switches to the job pane, and then to the console.
  • Needs to create another RStudio job to show the progress.

This is Unix only, for now. Windows is coming soon as well.

Unix only, for now.
@gaborcsardi gaborcsardi force-pushed the feature/rstudio-jobs branch from ab47c00 to 5d01aa7 Compare March 18, 2021 14:23
@gaborcsardi
Copy link
Member Author

This is quite difficult to implement on Windows, unfortunately. The closest I got is this, maybe this is doable with some more effort:

  1. Create a pipe server in the main process.
  2. Start RStudio job process, then wait.
  3. The job process connects to the pipe server.
  4. The job process tells the main process that it has connected to the pipe, and the main process can continue.
  5. The main process starts the subprocess, and passes the server end of the pipe to it.

The difficult step is 4, because reading from or writing to a pipe that does not have both ends connected is an error. So the main process cannot simply poll the read end of the pipe.

There is ConnectNamedPipe(), but this is a non-interruptible call, unless we use overlapped IO. But if we use overlapped IO and pass the overlapped pipe to the child process, then (in theory) the child process must also use overlapped IO when writing to the stdout. Which is quite a strong assumption. Luckily, doing a synchronous write on an overlapped handle seems to work and block internally. (It is potentially problematic if one mixes it with asynchronous calls, but the child process will probably not do that.) (https://community.osr.com/discussion/comment/225712/#Comment_225712)

In addition to the Windows difficulties, the other downside of the FIFO (or named pipe) approach is that the background process will die when trying to write to a terminated RStudio job process. So maybe it is better to use a temp file for this, and we can poll() the file on Unix and open the file with overlapped IO on Windows. If this works at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant