SessionState | R Documentation |
This class holds the state of a background session
used by an asynchronous backend (i.e., AsyncBackend
). See the
Details section for more information.
The session state is useful to check if an asynchronous backend is ready for certain operations. A session can only be in one of the following four states at a time:
session_is_starting
: When TRUE
, it indicates that the session is
starting.
session_is_idle
: When TRUE
, it indicates that the session is idle and
ready to execute operations.
session_is_busy
: When TRUE
, it indicates that the session is busy
(i.e., see the TaskState
class for more information about a
task's state).
session_is_finished
: When TRUE
, it indicates that the session is closed
and no longer available for operations.
session_is_starting
A logical value indicating whether the session is starting.
session_is_idle
A logical value indicating whether the session is idle and ready to execute operations.
session_is_busy
A logical value indicating whether the session is busy.
session_is_finished
A logical value indicating whether the session is closed and no longer available for operations.
new()
Create a new SessionState
object and determine the state
of a given background session
.
SessionState$new(session)
session
A callr::r_session
object.
An object of class SessionState
.
clone()
The objects of this class are cloneable with this method.
SessionState$clone(deep = FALSE)
deep
Whether to make a deep clone.
TaskState
, AsyncBackend
and
ProgressTrackingContext
.
# Handy function to print the session states all at once.
check_state <- function(session) {
# Create a session object and determine its state.
session_state <- SessionState$new(session)
# Print the state.
cat(
"Session is starting: ", session_state$session_is_starting, "\n",
"Session is idle: ", session_state$session_is_idle, "\n",
"Session is busy: ", session_state$session_is_busy, "\n",
"Session is finished: ", session_state$session_is_finished, "\n",
sep = ""
)
}
# Create a specification object.
specification <- Specification$new()
# Set the number of cores.
specification$set_cores(cores = 2)
# Set the cluster type.
specification$set_type(type = "psock")
# Create an asynchronous backend object.
backend <- AsyncBackend$new()
# Start the cluster on the backend.
backend$start(specification)
# Check that the session is idle.
check_state(backend$cluster)
{
# Run a task in parallel (i.e., approx. 0.25 seconds).
backend$sapply(
x = 1:10,
fun = function(x) {
# Sleep a bit.
Sys.sleep(0.05)
# Compute something.
output <- x + 1
# Return the result.
return(output)
}
)
# And immediately check that the session is busy.
check_state(backend$cluster)
}
# Get the output and wait for the task to complete.
output <- backend$get_output(wait = TRUE)
# Check that the session is idle again.
check_state(backend$cluster)
# Manually close the session.
backend$cluster$close()
# Check that the session is finished.
check_state(backend$cluster)
# Stop the backend.
backend$stop()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.