开发者

Send messages from within parallel function

I would like to send messages from within a function to the R console during a parallel process using the parallel开发者_如何学C package and pblapply::pblapply().

Here is a reprex that does not send any messages to the R console:

# library
library(pbapply)
library(stringi)
library(parallel)

# make fun
fun_func <- function(x){
  cat(paste0("hello world ",x))
  return(paste0("hello world ",x))}


# get data
set.seed(23)
d <- stri_rand_strings(100, 2, '[a-z]')
names(d) <- d

# make cluster
cl <- parallel::makeCluster(3)

# load func
clusterExport(cl, c("fun_func"))

# run function
pblapply(cl=cl,X=d,FUN=fun_func) -> res

# stop cluster
parallel::stopCluster(cl)

# show res
head(res)
#> $of
#> [1] "hello world of"
#> 
#> $is
#> [1] "hello world is"
#> 
#> $vl
#> [1] "hello world vl"
#> 
#> $zz
#> [1] "hello world zz"
#> 
#> $vz
#> [1] "hello world vz"
#> 
#> $ws
#> [1] "hello world ws"

Created on 2022-12-07 with reprex v2.0.2

Update: I just learned that it mitght be hard to get console message in Windows/RStudio. However, logging the messages with ParallelLogger might be an option. Unfortunaltely I could not implement it.

So I would be happy to have a solution either for sending the messages to the console or to a file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜