-
Notifications
You must be signed in to change notification settings - Fork 10
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
Comments
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 |
Hello lionel, Thanks for your reply !
In fact, waiting for all promises to complete before R exits just fits my needs. All the best |
@Fabrice-Clement I do something similar I include this at the bottom of my script. while (!later::loop_empty()) {
later::run_now()
} CC: @lionel- |
@dereckmezquita Interesting. Is this busy-looping or does |
@lionel- I did some digging into the behaviour of Indeed it was busy looping: while (!later::loop_empty()) {
later::run_now()
} If I understood correclty, busy looping means that On the other hand, a blocking call would wait until there is an event ready to be processed. For example, if run_now() had a blocking behaviour, it wouldn’t return until it had something to do, meaning the loop wouldn’t run continuously and hog the CPU. I believe we have two options here: 1. adding a small pause or using This reduces the polling frequency by inserting a brief delay. while (!later::loop_empty()) {
later::run_now()
Sys.sleep(0.001) # pause for 1 millisecond to lower CPU usage
} From the
while (!later::loop_empty()) {
later::run_now(timeoutSecs = Inf, all = TRUE)
} This way, the function waits for the next event rather than returning immediately. I apprecite your suggestion that got me to go look at the docs. Edit: this works nicely: # Simulate an asynchronous API call that returns a promise after a delay
get_data_async <- function(url) {
promises::promise(function(resolve, reject) {
# Simulate network delay of 1 second
later::later(function() {
resolve(paste("Data from", url))
}, delay = 10)
})
}
# Define an asynchronous main function
async_main <- coro::async(function() {
# Await the asynchronous API call
data <- await(get_data_async("http://example.com/api"))
cat("Received:", data, "\n")
})
# Kick off the asynchronous main function
async_main()
# Process the event loop until all tasks are completed
while (!later::loop_empty()) {
later::run_now(timeoutSecs = Inf, all = TRUE)
} |
Makes sense, thanks! |
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 :
Thanks for your advice !
The text was updated successfully, but these errors were encountered: