mirai
ミライ
みらい 未来 Minimalist Async Evaluation Framework for R → Designed for simplicity, a ‘mirai’ evaluates an R expression asynchronously in a parallel process, locally or distributed over the network.
→ Modern networking and concurrency, built on nanonext and NNG (Nanomsg Next Gen), ensures reliable and efficient scheduling over fast inter-process communications or TCP/IP secured by TLS. Distributed computing can launch remote resources via SSH or cluster managers.
→ A queued architecture readily handles more tasks than available processes, requiring no storage on the file system. Innovative features include event-driven promises, asynchronous parallel map, and automatic serialization of otherwise non-exportable reference objects.
Quick Start
Use mirai()
to evaluate an expression asynchronously in a separate,
clean R process.
The following mimics an expensive calculation that eventually returns a vector of random values.
library(mirai)
m <- mirai({Sys.sleep(n); rnorm(n, mean)}, n = 5L, mean = 7)
The mirai expression is evaluated in another process and hence must be self-contained, not referring to variables that do not already exist there. Above, the variables
n
andmean
are passed as part of themirai()
call.
A ‘mirai’ object is returned immediately - creating a mirai never blocks the session.
m
#> < mirai [] >
Whilst the async operation is ongoing, attempting to access a mirai’s data yields an ‘unresolved’ logical NA.
m$data
#> 'unresolved' logi NA
To check whether a mirai remains unresolved (yet to complete):
unresolved(m)
#> [1] TRUE
To wait for and collect the return value, use the mirai’s []
method:
m[]
#> [1] 6.288799 7.337810 6.767335 7.435713 7.628763
As a mirai represents an async operation, it is never necessary to wait
for it. Once it completes, the return value is automatically available
at $data
.
while (unresolved(m)) {
# do work here that does not depend on `m`
}
m$data
#> [1] 6.288799 7.337810 6.767335 7.435713 7.628763