"un-register" a doParallel cluster "un-register" a doParallel cluster r r

"un-register" a doParallel cluster


The only official way to "unregister" a foreach backend is to register the sequential backend:

registerDoSEQ()

This makes sense to me because you're supposed to declare which backend to use, so I didn't see any point in providing a way to "undeclare" which backend to use. Instead, you declare that you want to use the sequential backend, which is the default.

I originally considered including an "unregister" function, but since I couldn't convince myself that it was useful, I decided to leave it out since it's much easier to add a function than to remove one.

That being said, I think all you need to do is to remove all of the variables from foreach:::.foreachGlobals which is where foreach keeps all of its state:

unregister <- function() {  env <- foreach:::.foreachGlobals  rm(list=ls(name=env), pos=env)}

After calling this function, any parallel backend will be deregistered and the warning will be issued again if %dopar% is called.


    cl <- makeCluster(2)    registerDoParallel(cl)    on.exit(stopCluster(cl))

This worked fine for me.