简体   繁体   中英

Asynchronous child process in Windows

On Windows, the usual methods for parallelization in R (foreach, parLapply) will wait until their workers are all done before allowing the parent process to move on. I would like to start up a child process and NOT wait for it to finish its task before running my other code (running that child process only for its side effects). It seems like this behavior is available through the mcparallel function in the parallel package - but not on Windows, because it relies on forking! My job requires me to use Windows, so is there a Windows-friendly way to do this?

The future package (I'm the author) supports this in a cross-platform manner. For example,

library("future")
plan(multiprocess)
f1 <- future({ asynchronous code evaluated in the background })
f2 <- future({ some other asynchronous code })
v3 <- my_synchronous_code()
v1 <- value(f1)  # get value of f1 expression - wait if not done
v2 <- value(f2)  # get value of f2 expression - wait if not done
y <- f(v1, v2)

or equivalently using the future assignment operator:

library("future")
plan(multiprocess)
v1 %<-% { asynchronous code evaluated in the background }
v2 %<-% { some other asynchronous code }
v3 <- my_synchronous_code()
y <- f(v1, v2) # "use" value of v1 and v2 expressions - wait if not done

See also Running a R function in background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM