Evaluates a Job's Expression on a Background Process.
Evaluates a Job's Expression on a Background Process.
Active bindings
hooks
A named list of currently registered callback hooks.
backlog
A list of Jobs waiting to run on this Worker.
job
The currently running Job.
r_session
The
callr::r_session
background process interface.state
The Worker's state: 'starting', 'idle', 'busy', or 'stopped'.
loaded
A list of global variables and attached functions on this Worker.
uid
A short string, e.g. 'W11', that uniquely identifies this Worker.
Methods
Method new()
Creates a background callr::r_session
process for running Jobs.
Usage
Worker$new(
globals = NULL,
packages = NULL,
init = NULL,
hooks = NULL,
options = callr::r_session_options()
)
Arguments
globals
A list or similar set of values that are added to the
.GlobalEnv
of workers.packages
Character vector of package names to load on workers.
init
A call or R expression wrapped in curly braces to evaluate on each worker just once, immediately after start-up. Will have access to any variables defined by
globals
and assets frompackages
. Returned value is ignored.hooks
A list of functions to run when the Worker state changes, of the form
hooks = list(idle = function (worker) {...}, busy = ~{...})
. The names of these functions should bestarting
,idle
,busy
,stopped
, or'*'
.'*'
will be run every time the state changes, whereas the others will only be run when the Worker enters that state. Duplicate names are allowed.options
Passed to
callr::r_session$new()
Method print()
Print method for a Worker
.
Method start()
Starts a new background callr::r_session
process using the
configuration previously defined with Worker$new()
.
Method stop()
Stops a Worker by terminating the background callr::r_session
process
and calling <Job>$stop(reason)
on any Jobs currently assigned to this
Worker.
Method restart()
Restarts a Worker by calling <Worker>$stop(reason)
and
<Worker>$start()
in succession.
Method on()
Attach a callback function.
Arguments
state
The Worker state that will trigger this function. Typically one of:
'*'
-Every time the state changes.
'.next'
-Only one time, the next time the state changes.
'starting'
-After starting a new
callr::r_session
process.'idle'
-Waiting on new Jobs to be added.
'busy'
-After a Job starts running.
'stopped'
-After
<Worker>$stop()
is called.
func
A function that accepts a Worker object as input. You can call
<Worker>$stop()
or edit its values and the changes will be persisted (since Workers are reference class objects). You can also edit/stop the active or backlogged jobs by modifying<Worker>$job
or<Worker>$backlog
. Return value is ignored.
Method run()
Assigns a Job to this Worker for evaluation on its background
callr::r_session
process. Jobs can be assigned even when the Worker is
in the 'starting' or 'busy' state. A backlog of jobs will be evaluated
sequentially in the order they were added.
Arguments
job
A Job object, as created by
Job$new()
.