TaskState {parabar}R Documentation

TaskState

Description

This class holds the state of a task deployed to an asynchronous backend (i.e., AsyncBackend). See the Details section for more information.

Details

The task state is useful to check if an asynchronous backend is free to execute other operations. A task can only be in one of the following three states at a time:

The task state is determined based on the state of the background session (i.e., see the get_state method for callr::r_session) and the state of the task execution inferred from polling the process (i.e., see the poll_process method for callr::r_session) as follows:

Session State Execution State Not Started Is Running Is Completed
idle timeout TRUE FALSE FALSE
busy timeout FALSE TRUE FALSE
busy ready FALSE FALSE TRUE

Active bindings

task_not_started

A logical value indicating whether the task has been started. It is used to determine if the backend is free to execute another operation.

task_is_running

A logical value indicating whether the task is running.

task_is_completed

A logical value indicating whether the task has been completed and the output needs to be retrieved.

Methods

Public methods


Method new()

Create a new TaskState object and determine the state of a task on a given session.

Usage
TaskState$new(session)
Arguments
session

A callr::r_session object.

Returns

An object of class TaskState.


Method clone()

The objects of this class are cloneable with this method.

Usage
TaskState$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

AsyncBackend and ProgressTrackingContext.

Examples

# Handy function to print the task states all at once.
check_state <- function(session) {
    # Create a task state object and determine the state.
    task_state <- TaskState$new(session)

    # Print the state.
    cat(
        "Task not started: ", task_state$task_not_started, "\n",
        "Task is running: ", task_state$task_is_running, "\n",
        "Task is completed: ", task_state$task_is_completed, "\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 task has not been started (i.e., the backend is free).
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 the state to see that the task is running.
    check_state(backend$cluster)
}

# Sleep for a bit to wait for the task to complete.
Sys.sleep(1)

# Check that the task is completed (i.e., the output needs to be retrieved).
check_state(backend$cluster)

# Get the output.
output <- backend$get_output(wait = TRUE)

# Check that the task has not been started (i.e., the backend is free again).
check_state(backend$cluster)

# Stop the backend.
backend$stop()


[Package parabar version 1.1.1 Index]