cli icon indicating copy to clipboard operation
cli copied to clipboard

use cli progress bar in parallel context

Open thmschk opened this issue 3 years ago • 1 comments

I wonder if there is a possibilty to use cli::cli_progress_bar within a parallel backend:

library(dplyr)
library(foreach)
library(cli)

df <- data.frame(id =seq(1,100,1),random = seq(2,200,2))

cl <- parallel::makeForkCluster(5) 
doParallel::registerDoParallel(cl) 

cli::cli_progress_bar("Cleaning data", total = 100)

results <- foreach::foreach(i=unique(df$id),
														.combine =dplyr::bind_rows) %dopar% {
	data <- df %>% dplyr::filter(id == i) %>% 
		dplyr::mutate(result = random^2) # just a non-sense calculation

	cli::cli_progress_update()
	
	Sys.sleep(5/100)
	
	data
	
	}

parallel::stopCluster(cl)

# results

gives me

error in { : task 1 failed - "Cannot find current progress bar for <environment: 0x55fd5a223bf0>"

What do I have to consider in order to make the MWE work?

thmschk avatar Jun 01 '22 10:06 thmschk

Unfortunately that is not going to work, cli does not send progress messages between processes.

gaborcsardi avatar Jun 01 '22 10:06 gaborcsardi