coro
coro copied to clipboard
Running coro in R scripts
Hello,
Nice R package that I love to use now !
I would like to use coro in a R script, but when I run the example https://coro.r-lib.org/reference/async.html with Rscript, it ends immediatly.
How can I wait the termination of the promise before R exits ?
The only ( and dirty ) solution I found is :
# mycoroScript.R
# see https://coro.r-lib.org/reference/async.html
promises::promise_all( async_count_down(5), async_count_up(5) ) %>% then( function(value) {
writeLines("q( status = 0)", f <- file("/tmp/fifo", raw=T) ); close(f)
}
To run it from bash :
$ ( mkfifo /tmp/fifo; cat mycoroScript.R; cat /tmp/fifo) | R --interactive --no-save
Thanks for your advice !
Hello,
Unfortunately this is not officially possible with promises. That's something that I would like to become possible in the future though. If you'd like to use an unsupported workaround in the meantime, see the implementation of wait_for()
in https://github.com/r-lib/coro/blob/main/R/utils-promises.R
Hello lionel,
Thanks for your reply ! After reading your wait_for code and the fact that it is based on later, I do that :
.Last <- while( ! later::loop_empty() ) later::run_now(timeoutSecs = Inf, all = TRUE, loop = later::global_loop())
In fact, waiting for all promises to complete before R exits just fits my needs.
All the best