I have about 20 typical functions like:
echo_delayed <- function(j){
jj <<- j # for debugging purpose
Sys.sleep(1.5)
print(paste0('echo_delayed - ', now() %>% toString(), " ", j))
}
All of them have j
parameter that is an embraced JSON string parsed in these functions. The functions produce different kinds of outputs. Typically ggplot()... %>% print()
, string or byte stream. These functions are presented by plumber
. For instance:
#* @post /echo_delayed
#* @serializer contentType list(type="text/html; charset=UTF-8")
echo_delayed
Using this sample I am trying to wrap some of them to work in a non-blocking mode:
time <- function(name_, name = name_) {
paste0(name, ": ", Sys.time())
}
new_promise <- function() {
promise(function(resolve, reject){ resolve(NULL) })
}
add_echo_delayed <- function(p) {
p <- then(p, function(value) {
echo_delayed(value)
"" # return value
})
p
}
#' @post /echo_delayed
#' @serializer contentType list(type="text/html; charset=UTF-8")
function() {
new_promise() %>%
add_echo_delayed() %...>%
time("echo_delayed")
}
However it is not running in a separate thread and other queries are waiting until echo_delayed()
is done with calculation.
The functions from above mentioned sample (with minimal edits) are working fine:
sleep_count <- 5
add_async_sleep <- function(p) {
n <- 20
for (i in 1:(sleep_count * n)) {
p <- then(p, function(value) {
Sys.sleep(1/n)
"" # return value
})
}
p
}
#' @post /async_post
#' @serializer contentType list(type="text/html; charset=UTF-8")
function() {
new_promise() %>%
add_async_sleep() %...>%
time("async_post")
}
It stops working if I break splitting the sample function into pieces:
add_async_sleep2 <- function(p) {
p <- then(p, function(value) {
Sys.sleep(sleep_count)
"" # return value
})
p
}
I am stuck. Could anyone please help me to properly wrap the function echo_delayed()
so that it starts running in a separate thread and the parameter j
is passed into it?