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

Running coro in R scripts #34

Open
Fabrice-Clement opened this issue Mar 4, 2021 · 2 comments
Open

Running coro in R scripts #34

Fabrice-Clement opened this issue Mar 4, 2021 · 2 comments

Comments

@Fabrice-Clement
Copy link

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 !

@lionel-
Copy link
Member

lionel- commented Mar 4, 2021

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

@Fabrice-Clement
Copy link
Author

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

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

No branches or pull requests

2 participants