Can progressr handlers receive partial results from future_lapply? #121
-
First, thank you so much for all of your work on Futureverse. I'm really excited to use this framework for developing packages to get consistent (yet flexible) behavior on all OS's. Background (feel free to skip): I'm working on a package to facilitate Monte Carlo simulations and am planning to rely on Below is a serial (and inefficient for several reasons) example to give an idea of what I hope to allow the user to do. I would like to achieve something like the example using
I wonder if After writing this up, it feels like my desired behavior could be an abuse of the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
@HenrikBengtsson Do you know if this is currently possible? |
Beta Was this translation helpful? Give feedback.
-
Sorry for missing replying to this one.
My approach would be to first rewrite your for loop as a # hook1 and hook2 will be called every 'hook_freq' sims.
hook_freq <- 5000
res <- future_lapply(seq.int(50000), FUN = function(i) {
res_i <- rnorm(n = 1, mean = 0, sd = 100)
if (i %% hook_freq == 0) {
hook1(res_i)
hook2(res_i)
}
res_i
})
The above rewrite should do exactly this, regardless of your parallel backend and how many workers you have. It will call the hook functions with the frequency you request. Note that, since it runs in parallel, you might of course see RDS files being saved out of order. If you think about it, there's nothing with your need to cache results to the file system that is related to parallelization, or the future framework; your problem is exactly the same when you run sequentially with
Nah, progressr does not support this, and, yes, it would be "abusing" it. The longer answer is that, although your gut feeling is that one could use a similar framework to progressr to send other type of information from parallel workers back to the parent R session, progressr can only signal a character string message. So, unless you encode your information into a character string, and then write your own progressr handler to decode that (and do what ever you want with that info), then progressr is not the way to go here. |
Beta Was this translation helpful? Give feedback.
-
Do you mean that I should keep the code as is in #121 (reply in thread) where I manually split the chunks, and have an outer for loop (that is not parallelized) ? Or are you referring to your code, in the part
This makes me think you're talking about your code that uses
Makes sense.
Sounds good, I'll give this a shot once I understand how to do the first step. |
Beta Was this translation helpful? Give feedback.
Do you mean that I should keep the code as is in #121 (reply in thread) where I manually split the chunks, and have an outer for loop (that is not parallelized) ? Or are you referring to your code, in the part
if (i %% hook_freq == 0) {
?This makes me think you're talking about your code that uses
if (i %% hook_freq == 0) {
since I indeed understand that would cau…