Where Job expressions are evaluated.
Active bindings
hooksA named list of currently registered callback hooks.
jobThe currently running Job.
psThe
ps::ps_handle()object for the background process.stateThe Worker's state:
'starting','idle','busy', or'stopped'.uidA short string, e.g.
'W11', that uniquely identifies this Worker.tmpThe Worker's temporary directory.
cndThe error that caused the Worker to stop.
Methods
Method new()
Creates a background R process for running Jobs.
Usage
Worker$new(
globals = NULL,
packages = NULL,
namespace = NULL,
init = NULL,
hooks = NULL,
wait = TRUE,
timeout = Inf
)Arguments
globalsA named list of variables that all
<Job>$exprs will have access to. Alternatively, an object that can be coerced to a named list withas.list(), e.g. named vector, data.frame, or environment.packagesCharacter vector of package names to load on workers.
namespaceThe name of a package to attach to the worker's environment.
initA call or R expression wrapped in curly braces to evaluate on each worker just once, immediately after start-up. Will have access to variables defined by
globalsand assets frompackagesandnamespace. Returned value is ignored.hooksA named list of functions to run when the Worker state changes, of the form
hooks = list(idle = function (worker) {...}). Names of worker hooks are typicallystarting,idle,busy,stopped, or'*'(duplicates okay). Seevignette('hooks').waitIf
TRUE, blocks until the Worker is 'idle'. IfFALSE, the Worker object is returned in the 'starting' state.timeoutHow long to wait for the worker to finish starting (in seconds). If
NA, defaults to theWorker$new()argument.
Method print()
Print method for a Worker.
Method start()
Restarts a stopped Worker.
Method stop()
Stops a Worker by terminating the background 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.
Arguments
waitIf
TRUE, blocks until the Worker is 'idle'. IfFALSE, the Worker object is returned in the 'starting' state.timeoutHow long to wait for the worker to finish starting (in seconds). If
NA, defaults to theWorker$new()argument.reasonPassed to
<Job>$stop()for any Jobs currently managed by this Worker.clsPassed to
<Job>$stop()for any Jobs currently managed by this Worker.
Method on()
Attach a callback function to execute when the Worker enters state.
Arguments
stateThe name of a Worker state. Typically one of:
'*'- Every time the state changes.'.next'- Only one time, the next time the state changes.'starting'- Waiting for the background process to load.'idle'- Waiting for Jobs to be$run().'busy'- While a Job is running.'stopped'- After<Worker>$stop()is called.
funcA function that accepts a Worker object as input. You can call
<Worker>$stop()and other<Worker>$methods.
Method wait()
Blocks until the Worker enters the given state.
Arguments
stateThe name of a Worker state. Typically one of:
'*'- Every time the state changes.'.next'- Only one time, the next time the state changes.'starting'- Waiting for the background process to load.'idle'- Waiting for Jobs to be$run().'busy'- While a Job is running.'stopped'- After<Worker>$stop()is called.
timeoutStop the Worker if it takes longer than this number of seconds.
signalRaise an error if encountered (will also be recorded in
<Worker>$cnd).
Method run()
Assigns a Job to this Worker for evaluation on the background process.
Arguments
jobA Job object, as created by
Job$new().