Waitgroups in R Programming Language

# To wait for multiple tasks to finish, we can use the `parallel` package

library(parallel)
library(futile.logger)

# This is the function we'll run in every task
worker <- function(id) {
  flog.info("Worker %d starting", id)
  
  # Sleep to simulate an expensive task
  Sys.sleep(1)
  flog.info("Worker %d done", id)
}

# Main function
main <- function() {
  # Create a cluster with 5 workers
  cl <- makeCluster(5)
  
  # Launch several tasks
  tasks <- lapply(1:5, function(i) {
    function() worker(i)
  })
  
  # Execute tasks in parallel and wait for all to finish
  results <- parLapply(cl, tasks, function(task) task())
  
  # Stop the cluster
  stopCluster(cl)
}

# Run the main function
main()

This R script demonstrates a similar concept to Go’s WaitGroups using the parallel package. Here’s a breakdown of the code:

  1. We import the necessary libraries: parallel for parallel processing and futile.logger for logging.

  2. The worker function simulates a task that takes some time to complete. It logs when it starts and finishes.

  3. In the main function:

    • We create a cluster with 5 workers using makeCluster().
    • We create a list of tasks, each calling the worker function with a different ID.
    • We use parLapply() to execute these tasks in parallel and wait for all of them to finish.
    • Finally, we stop the cluster with stopCluster().
  4. We call the main function to run our parallel tasks.

To run this script, save it as parallel_tasks.R and use:

$ Rscript parallel_tasks.R

The output will show workers starting and finishing, likely in a different order each time:

INFO [2023-06-08 10:15:30] Worker 1 starting
INFO [2023-06-08 10:15:30] Worker 2 starting
INFO [2023-06-08 10:15:30] Worker 3 starting
INFO [2023-06-08 10:15:30] Worker 4 starting
INFO [2023-06-08 10:15:30] Worker 5 starting
INFO [2023-06-08 10:15:31] Worker 1 done
INFO [2023-06-08 10:15:31] Worker 2 done
INFO [2023-06-08 10:15:31] Worker 3 done
INFO [2023-06-08 10:15:31] Worker 4 done
INFO [2023-06-08 10:15:31] Worker 5 done

Note that R’s parallel processing model is different from Go’s goroutines. R uses separate processes for parallelism, which can be more resource-intensive but allows for true parallel execution on multiple cores. The parallel package provides a way to manage these processes and wait for their completion, similar to how WaitGroups work in Go.

For more advanced use cases, you might want to look into the future package, which provides a unified interface for parallel and distributed processing in R.