3

I need my script to wait until another process (which is not a child process of my R session) finishes. How can I do this in R?

The only thing I found is wait function in bfork package, but this package was removed from CRAN.

Then I found package processx, which has function poll, but this only seems to be working on processes spawned by the R session.

PS: I am working on Windows, but OS-independent solution is highly preferable.

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • Are you comfortable looping (in a polling sleep) on a `system` call? It's os-specific, unfortunately, so while `grepl("^\\s*1234\\s", system("ps"))` works on linux, on windows you'll need `grepl("^\\s*1234\\s", system("ps -s"))`. This isn't fully robust (as a new process with the same PID will fool you), you might put in more robust checks/regexes. – r2evans Jun 06 '19 at 16:54
  • Is there a reason you cannot use the code from `bfork`? While no longer on CRAN, the [source](https://github.com/cran/bfork) is still available, at least for now. – r2evans Jun 06 '19 at 17:27
  • @r2evans yeah, active polling is the last resort but will do the job as well. However, `ps` is not present on Windows. And hmmm, looking at the [very short source of bfork](https://github.com/cran/bfork/blob/master/src/fork.c), it looks they only support Linux/UNIX as well.. – Tomas Jun 06 '19 at 17:30
  • I hadn't noticed `bfork`'s linux-reliance, sorry I didn't dive that deeply. Perhaps https://stackoverflow.com/a/5487814 can help? It'd require you to compile and bind in R. I understand completely about not wanting to poll ... – r2evans Jun 06 '19 at 17:33
  • 1
    @r2evans that seems to be working for child processes only. – Tomas Jun 06 '19 at 17:37
  • @r2evans My bad. Here's the correct link. Possibly related - [R function to determine if another application is running](https://stackoverflow.com/questions/56200530/r-function-to-determine-if-another-application-is-running) – Shree Jun 06 '19 at 18:43
  • 1
    Yeah, that's what I was thinking ... both that windows would require `tasklist`, and that it will need to be polled. – r2evans Jun 06 '19 at 18:58
  • this could possibly be a solution in `C++` for someone who would like to write a package (not me :)) https://stackoverflow.com/questions/35271993/how-to-wait-for-a-non-child-process-with-winapi – Tomas Jun 07 '19 at 05:35

0 Answers0